查询多个帖子类型和类别

时间:2018-07-27 作者:Lust

我有一个div,里面我希望随机显示一个来自a类的帖子或一个来自b类的产品。我正在寻找一个查询来做到这一点。

我试过这个:

 <?php
        $args = array( 
        \'post_type\' => array(\'product\', \'post\'), 
        \'posts_per_page\' => 1, 
        \'cat\' => 1,2, 
        \'orderby\' => \'RAND\' 
        );

        $loop = new WP_Query( $args );       

        while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

            <article class="promo">  

                    <div class="image">
                    <?php if (is_singular(\'product\')){
                        echo \'Product\';

                    }else{
                        echo \'Post\';
                    }?>
                    </div>  


                </article>
<?php endwhile; ?>
有人知道怎么做?谢谢:)

EDIT CODE

    <?php
$args = [
    \'post_type\' => [\'product\',\'post\'],
    \'posts_per_page\' => -1,
    \'fields\' => \'ids\',
     \'post_type\' => [\'product\',\'post\'],
\'posts_per_page\' => 2,
\'fields\' => \'ids\',
\'tax_query\' => [

    \'relation\' => \'OR\',
    \'taxonomy\' => \'category\',
    \'field\' => \'slug\',
    \'terms\' => [\'promo-distributeur\', \'actu-distrib\'],
    \'include_children\' => false,

]
    ]
];
$posts_ids = get_posts($args); 
$total = count($posts_ids);


if ($total > 0) { 
    $rnd = mt_rand(0, $total);
    $pid = $posts_ids[$rnd];
    $post = get_post( $pid);



while ( $post->have_posts() ) : $post>the_post(); global $product; ?>

            <article class="promo">  

                    <div class="image">
                    <?php if (is_singular(\'product\')){
                        echo \'Product\';

                    }else{
                        echo \'Post\';
                    }?>
                    </div>  


                </article>
<?php endwhile;
} ?>    

2 个回复
SO网友:nmr

以下代码选择所有POST类型的IDproductpost, 属于术语的category-acategory-b, 具有taxonomy_01taxonomy_02, 分别地

$args = [
    \'post_type\' => [\'product\',\'post\'],
    \'posts_per_page\' => -1,
    \'fields\' => \'ids\',
    \'tax_query\' => [
        \'relation\' => \'OR\',
        [
            \'taxonomy\' => \'taxonomy_01\',
            \'field\' => \'slug\',
            \'terms\' => \'category-a\',
            \'include_children\' => false,
        ],
        [
            \'taxonomy\' => \'taxonomy_02\',
            \'field\' => \'slug\',
            \'terms\' => \'category-b\',
            // OR by id
            //\'field\' => \'term_id\', // default value
            //\'terms\' => 2,
            \'include_children\' => false,
        ],
    ]
];
$posts_ids = get_posts($args); 
$total = count($posts_ids);
if ($total > 0) { 
    $rnd = mt_rand(0, $total - 1);
    $pid = $posts_ids[$rnd];
    $my_post = get_post( $pid);
    // display post
}
我假设条款category-a, category-b 属于不同的分类法。如果没有,则tax_query 应为:

\'tax_query\' => [
    [
        \'taxonomy\' => \'taxonomy_01\',
        \'field\' => \'slug\',
        \'terms\' => [\'category-a\', \'category-b\'],
        \'include_children\' => false,
    ],
]
UPDATE
我更改了上述代码中的变量$postmy_post. 您的代码:

$args = [
    \'post_type\' => [\'product\',\'post\'],
    \'posts_per_page\' => -1,
    \'fields\' => \'ids\',
    \'tax_query\' => [
        \'relation\' => \'OR\',
        [
            \'taxonomy\' => \'taxonomy_01\',
            \'field\' => \'slug\',
            \'terms\' => \'category-a\',
            \'include_children\' => false,
        ],
        [
            \'taxonomy\' => \'taxonomy_02\',
            \'field\' => \'slug\',
            \'terms\' => \'category-b\',
            // OR by id
            //\'field\' => \'term_id\', // default value
            //\'terms\' => 2,
            \'include_children\' => false,
        ],
    ]
];
$posts_ids = get_posts($args); 
$total = count($posts_ids);
if ($total > 0) {
    // get random post_ID 
    $rnd = mt_rand(0, $total - 1);
    $pid = $posts_ids[$rnd];
    $my_post = get_post( $pid);

    // display post
    if ( !empty($my_post) ) : ?>

        <article class="promo">  
            <div class="image">
            <?php 
                if ( $my_post->post_type == \'product\' )
                    echo \'Product\';
                else
                    echo \'Post\';
            ?>
           </div>  
       </article>
   <?php endif;
}

SO网友:Andrea Somovigo

您不需要全局$product,并且is\\u singular()是一个模板函数,在循环中不可用,而且如果您只选择了1篇文章,那么orderby rand将不起作用,因为记录的子集总是相同的1元素

<div class="image">
   <?php 
    if ($post->post_type==\'product\'){
      echo \'Product\';
       }
   else{
        echo \'Post\';
        }?>
 </div> 

结束

相关推荐

使用jQuery显示/隐藏DIV-不能在一个页面中工作

两天来我只和一件事斗争。。。但我只是放弃了!我已经做了一个JSFIDLE来检查它,可能是我的代码错了或其他什么。。但是该代码在JSFIDLE上运行良好,但在我的this页面(在另一个子页面上,该代码运行正常)上则不行。。。我使用WPJob Manager Resumes... 我按代码生成页面[简历]按插件WPBakery Page Builder 我将自定义JS添加到子页面。所有代码的工作方式都类似于:alert(“test”),但不适用于以下情况: <script> jQue