dpyu7978 2014-09-23 15:12
浏览 70
已采纳

Wordpress query_posts多个数组?

Does anyone know if it is possible to have multiple arrays within query_posts if so how?

I'm looking to query posts that are in category 1 & 2. But also posts that are in 1 & 3 and 1 & 4.

So the posts MUST be in category 1 as well as one of the other categories.

So the category__and would be where I've started which allows me to query posts both in 1 & 2 but how would I then progress to add the others 3,4 etc? as adding them 1,2,3,4,5 will only show posts in all 5 categories correct?

Here is what I have so far:

<?php                                   
    query_posts( 
        array( 'category__and' => array(1,2), 
        'posts_per_page' => 5, 
        'orderby' => 'date', 
              ) );
    while (have_posts()) : the_post();  
?>

Is it possible to put an array of arrays to perform what I have asked?

  • 写回答

1条回答 默认 最新

  • douyinzha5820 2014-09-23 15:50
    关注

    This should be possible with the tax_query parameter.

    query_posts(
        array(
            posts_per_page => 5,
            tax_query => array(
                relation => 'OR',
                array(
                    'taxonomy' => 'category',
                    'operator' => 'IN',
                    'field' => 'id',
                    'terms' => array( 1, 2 ),
                ),
                array(
                    'taxonomy' => 'category',
                    'operator' => 'IN',
                    'field' => 'id',
                    'terms' => array( 1, 3 ),
                ),
                array(
                    'taxonomy' => 'category',
                    'operator' => 'IN',
                    'field' => 'id',
                    'terms' => array( 1, 4 ),
                ),
            ),
        )
    );
    

    See the documentation for any additional details: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?