从WP_QUERY中获取10个帖子。如果少于10,则从其他地方获得剩余部分

时间:2014-03-06 作者:tmyie

我正在尝试创建一个相关的posts WP\\u查询。到目前为止,我已经创建了一个自定义的WP\\u查询,该查询查询与标记数组匹配的所有帖子。

然而,我试图创建一个简单的if statement: 如果获取的帖子数为less than 10, 从中获取余数elsewhere (这可能来自特定类别)。

我有以下循环。它使用$related_posts->found_posts 获取帖子数量。

$related_posts_args =
    array(
    \'tag__and\' => $list_of_tags, // array of tag IDS
    \'posts_per_page\' => 10,
    \'post__not_in\' => $already_posted, // array of post IDs
    \'post_type\' => \'post\',
    \'post_status\' => \'publish\',
    \'orderby\' => \'date\',
    \'order\' => \'DESC\',
    \'orderby\' => \'rand\',
);

$related_posts_query = new WP_Query( $related_posts_args );
if ( $related_posts_query->have_posts() ):
    while ( $related_posts_query->have_posts() ):
        $related_posts_query->the_post();
// this is the number of posts in my current query
    echo $related_posts_query->found_posts; 
            the_title();
    endwhile;
endif;
wp_reset_postdata();
有人知道如何利用剩余的资源从其他地方获取帖子吗?(并且可能在同一个循环中)。

2 个回复
SO网友:RRikesh

您可以这样做:

$related_posts_query = new WP_Query( $related_posts_args );
if( $related_posts_query->found_posts < 10 ){
   $args = array(/* new wp_query args*/);
   $newquery = new WP_Query( $args );
}

# merge the two results
$related_posts_query->posts = array_merge( $related_posts_query->posts, $newquery->posts );
$related_posts_query->post_count = count( $related_posts_query->posts );

# do your loop here

SO网友:Pieter Goosen

IMO最好的方法是根据集合统计每页返回的帖子posts_per_page 在查询中设置选项。如果计数小于集合posts_per_page, 计算差额,然后将其用作在自定义查询中填充其余内容的帖子数量

这就是它的工作原理:

首先,计算“main”查询返回的帖子数量,在这种情况下,这将是$related_posts_args->posts

count($related_posts_args->posts)
因为您已经设置了posts_per_page10, 这将是用来检查计数的数字。

count($related_posts_args->posts) < 10
如果计数小于10,则需要获得10和计数之间的差值。这种差异将是posts_per_page 自定义查询选项

$ppp = 10 - count($related_posts_args->posts);
您现在可以运行自定义查询来检索帖子以填充空白。您只需要添加自己的查询参数

这是完整的代码

if( count($related_posts_args->posts) < 10 ) { 

    $ppp = 10 - count($related_posts_args->posts);

    $args = [ //Add your own query arguments to suite your needs
        \'orderby\' => \'rand\',
        \'posts_per_page\' => $ppp
    ];

    $q = new WP_Query( $args );

    if( $q->have_posts() ) {
        while( $q->have_posts() ) {
            $q->the_post();

            //Display your loop elements

        }
        wp_reset_postdata();
    }
}
与您的代码集成

$related_posts_args = [
    \'tag__and\' => $list_of_tags, // array of tag IDS
    \'posts_per_page\' => 10,
    \'post__not_in\' => $already_posted, // array of post IDs
    \'post_type\' => \'post\',
    \'post_status\' => \'publish\',
    \'orderby\' => \'date\',
    \'order\' => \'DESC\',
    \'orderby\' => \'rand\',
];

$related_posts_query = new WP_Query( $related_posts_args );
if ( $related_posts_query->have_posts() ) {
    while ( $related_posts_query->have_posts() ) {
        $related_posts_query->the_post();

        //Display your loop elements

    }
    wp_reset_postdata();

    if( count($related_posts_args->posts) < 10 ) { 

        $ppp = 10 - count($related_posts_args->posts);

        $args = [ //Add your own query arguments to suite your needs
            \'orderby\' => \'rand\',
            \'posts_per_page\' => $ppp
        ];

        $q = new WP_Query( $args );

        if( $q->have_posts() ) {
            while( $q->have_posts() ) {
                $q->the_post();

                //Display your loop elements

            }
            wp_reset_postdata();
        }
    }
}

结束

相关推荐

Multiple loops in Genesis

所以我想在我的主页上创建两个循环。一个可以显示页面自身内容的页面,然后在其正下方有一个网格循环,用于从“新闻”类别中获取两篇最新帖子。我真的找不到一种方法来做这件事,所以我四处黑客攻击,直到我让它工作起来,但我不确定这是正确的做法,有人能告诉我我可以/应该对这段代码做些什么改进吗?以下是我的截图:http://s18.postimg.org/knrq6sert/2013_11_13_22_55_58.png主题:MetroTemplate:主页。php//* Add support for Genesis