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

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

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源