Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress(嵌套)Post查询,按类别循环,然后按ACF循环_Php_Wordpress_Custom Wordpress Pages - Fatal编程技术网

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

Php WordPress(嵌套)Post查询,按类别循环,然后按ACF循环,php,wordpress,custom-wordpress-pages,Php,Wordpress,Custom Wordpress Pages,我会先告诉你我的目标,而不是我目前的目标 我的目标: 我制作了一个使用VisualComposer的快捷代码 在VC中,我可以检查此posttype的类别 简短代码的目标是首先根据所选类别对帖子进行选择和排序 第二,按照ACF进行划分和排序。说“自定义字段1” 第三也是最后一点。按字母顺序在“custom_field_2”和“custom_field_3”上对帖子进行排序 示例: 第一类 红色的 职位1 职位2 蓝色的 职位3 邮政4 第2类 红色的 邮政5 邮政6 蓝

我会先告诉你我的目标,而不是我目前的目标

我的目标:

  • 我制作了一个使用VisualComposer的快捷代码
  • 在VC中,我可以检查此posttype的类别
  • 简短代码的目标是首先根据所选类别对帖子进行选择和排序
  • 第二,按照ACF进行划分和排序。说“自定义字段1”
  • 第三也是最后一点。按字母顺序在“custom_field_2”和“custom_field_3”上对帖子进行排序

示例:

  • 第一类
    • 红色的
      • 职位1
      • 职位2
    • 蓝色的
      • 职位3
      • 邮政4
  • 第2类
    • 红色的
      • 邮政5
      • 邮政6
    • 蓝色的
      • 邮政7
      • 邮政8

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

<?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
}

内部ID:


好的,我找到了答案。 我将在这里为任何寻求解决方案的人发布。:)

步骤:

  • 需要一个包含“字段1”中所有唯一值的数组
  • 对每个帖子运行帖子查询,获取“字段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; 

祝你们今天愉快,孩子们

好吧,我找到了答案。 我将在这里为任何寻求解决方案的人发布。:)

步骤:

  • 需要一个包含“字段1”中所有唯一值的数组
  • 对每个帖子运行帖子查询,获取“字段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; 
祝你们今天愉快,孩子们