WP_QUERY中有多个值:CATEGORY__AND

时间:2017-12-22 作者:washoc

我希望有人能帮助我,因为我已经搜索了,但找不到答案。。。

我正在使用一个快捷码中的循环来显示相关项的列表,并且我很难理解在and参数中选择多个类别的逻辑。

例如,我有这样一个类别结构:

Products (id: 1)
   Prod A (id: 2)
   Prod B (id: 3)
Services (id: 4)
   Service A (id: 5)
   Service B (id: 6)
Overview (id: 7)
我想做的是显示所有分类为id 7和特定其他id的帖子。

例如:

\'category__and\' => array(7,2),
AND 
\'category__and\' => array(7,5),
这将显示2个结果-分类为(概述和产品A)和(概述和服务A)的帖子。

如果我只想显示一个选项(如Overview和Prod A),那么一切都可以,但我无法确定如何显示倍数。

将其更改为中的category\\uu不起作用,因为在上面的示例中,这显示了所有分类为Overview的帖子,包括Prod B和Service B。使用category\\uu not\\u in来排除类别也不切实际,因为列表很长并且会发生变化(除非有一种方法可以通过编程确定要排除哪些类别?)。

是否有一种方法可以使用多对category\\u和值,或者有一种方法可以为WP\\u查询编码多个循环来实现这一点?

我希望这能让你继续下去。提前感谢!

2 个回复
最合适的回答,由SO网友:washoc 整理而成

感谢您的建议,感谢@locomo建议合并结果。我的解决方案的逻辑是:

创建一个空数组来保存页面ID:$buildlist=array()

add_shortcode( \'related_overview\', \'fn_related_overview\' );
function fn_related_overview( $atts ) {

    ob_start();

      //set up array to hold page IDs which is built from looping queries of related items from custom field
      $buildlist = array();

      //get related items from custom field and add them to the array
      $items = get_field(\'related_overview\');

  // define attributes and their defaults
    extract( shortcode_atts( array (
        \'posts\' => -1,
        \'columns\' => 4, //1-6
    ), $atts ) );

      //loop through values in custom field and add page ids to merged array to use in final query
      foreach($items as $post) {
          $relatedcatid = $post->term_id;

    //use category__and to specify overview category (ID 64) AND others in a loop
    $options1 = array(
        \'post_type\' => \'page\',
        \'orderby\' => \'date\',
        \'posts_per_page\' => $posts,
        \'category__and\' => array(64,$relatedcatid),
    );

            $query1 = new WP_Query( $options1 );
            while ( $query1->have_posts() ) : $query1->the_post();
              //add page ID to the buildlist                    
              $buildlist[] = get_the_ID();
            endwhile;

    }//end foreach

      //remove duplicates from array of page ids in case they are tagged to multiple categories
      $buildlist = array_unique($buildlist);

//Set up main query using page IDs from buildlist
    $options = array(
        \'post_type\' => \'page\',
        \'orderby\' => \'date\',
        \'posts_per_page\' => $posts,
        \'post__in\' => $buildlist,
    );


        //run main query and loop through results
  $query = new WP_Query( $options );

    if ( $query->have_posts() ) { 
    ?>

HTML BEFORE LOOP HERE

            <?php while ( $query->have_posts() ) : $query->the_post();          ?> 

HTML INSIDE LOOP HERE
<?php echo the_title(); ?>

            <?php endwhile;
            wp_reset_postdata(); ?>

HTML AFTER LOOP HERE

        <?php $myvariable = ob_get_clean();
    return $myvariable;
    }//end if has posts
} //end function
我希望这有帮助。谢谢

SO网友:locomo

从你的问题中,我不太清楚标准是否必须与产品和服务相匹配。。或产品或服务。您可以更改“relation”参数以实现此目的(AND或or)

$args = array(
    \'posts_per_page\'  => -1,
    \'cat\' => 7,
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'key\'     => \'product_customfield\',
            \'value\'   => 2,
            \'compare\' => \'=\',
        ),
        array(
            \'key\'     => \'service_customfield\',
            \'value\'   => 5,
            \'compare\' => \'=\',
        ),
    ),
);
$query = new WP_Query( $args );

结束

相关推荐

WP_LIST_CATEGORIES()-将div添加到每个li?

我正在使用wp_list_categories() 从特定的作者ID打印出每个类别的链接,这很好。然而,我还想添加一个div 致各li 将保留类别的图标。我怎样才能做到这一点?这是我当前的代码:<?php // Display a list of all categories associated with author $cat_array = array(); $args = array( \'author\' =>