WordPress(嵌套)Post 查询,按类别循环,然后按 ACF 循环

WordPress (nested) Post query, loop per category and then per ACF

我先说说我的目标,而不是我目前的目标。

我的目标:


示例:


好的。我自己先试过了。到目前为止,这是我的代码:

<?php
function productlist_sc($atts){?>

    <?php
    global $post;

    $categories_array = array();
    $thecategories = get_categories();
    foreach( $thecategories as $category ){
        $categories_array[] = $category->slug;
    }

    if($atts[ 'category' ]){
        $atts[ 'category' ] = explode( ",", $atts[ 'category' ] );
    }

    //collect values, combining passed in values and defaults
    $values = shortcode_atts(array(
            'category' => ''
        ),$atts);

    $categories = get_categories( array(
            'orderby' => 'name',
            'parent'  => 0,
            'slug'    => $values['category']
        ) );

    $current = get_the_ID($post->ID);
    $cargs = array(
        //'child_of'      => 0,
        'orderby'       => 'name',
        'parent'   => 0,
        'order'         => 'ASC',
        'hide_empty'    => 1
        //'taxonomy'      => 'category', //change this to any taxonomy
    );

    // first sort all selected posts per category
    foreach ( $categories as $tax ) :



        // List posts by the terms for a custom taxonomy of any post type
        $args = array(
            'post_type' => 'products',
            'orderby' => 'ASC',
            'posts_per_page'=>-1,
            'category_name' => $tax->slug
        );


    $the_query = new WP_Query( $args ); ?>
            <?php if ( $the_query->have_posts() ) : ?>

                <h2>Internal ID: <?php echo $tax->name; ?></h2><?php

    // second sort all selected posts per custom_field_1
    $mykey_values = get_post_custom_values( 'custom_field_1' );
    foreach ( $mykey_values as $key => $value ) :
        ?><h3><?php the_field('custom_field_1'); ?></h3>

                    <div class="rtt_prod_table">

                <!-- the loop -->
                <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

                            <div>
                                <?php if( get_field('custom_field_1') || get_field('custom_field_2') || get_field('custom_field_3') ): ?>
                                    <ul>
                                        <?php if( get_field('custom_field_2') ): ?>
                                            <li>
                                                <?php the_field('custom_field_2'); ?>
                                            </li>
                                        <?php endif; ?>
                                        <?php if( get_field('custom_field_3') ): ?>
                                            <li>
                                                <?php the_field('custom_field_3'); ?>
                                            </li>
                                        <?php endif; ?>

                                    </ul>
                                <?php endif; ?>
                            </div>

                        </div>
                    </div>

                    <?php endwhile; ?> <!-- end of the loop -->

                <div><!-- end .accord-content -->
            </div>
            <?php wp_reset_postdata();
    endforeach; //  custom_field_1
?>
            <?php else : ?>
                <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
            <?php endif; ?>

            <?php

    endforeach; /* end $tax */ ?>

<?php
}

好的,我找到答案了。 我会 post 在这里为任何寻找解决方案的人提供。 :)

步骤:

  • 需要一个包含 "field 1"
  • 中所有唯一值的数组
  • 运行 每个 post 的 post 查询,抓取 "field 1" 的值并放入数组
  • 过滤了所有重复项
  • 按字母顺序排列数组
  • 运行 数组中的一个foreach:

这是我在上面的代码中添加的代码

$stack = array();

// query
$args = array(
    'post_type' => 'products',
    'orderby' => 'ASC',
    'posts_per_page'=>-1,
    'category_name' => $tax->slug
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();

        if( get_field('custom_field_1') ): 
            $stack[] = get_field('custom_field_1');
        endif;
    }
    wp_reset_postdata();
}   

$result = array_unique($stack);
sort($result);

foreach ( $result as $tax2 ) :
     // run you code here
endforeach; 

祝男孩女孩们今天愉快!