仅在Wordpress中查询粘贴帖子

时间:2016-01-14 12:27:05

标签: wordpress sticky wp-query

我有以下查询来收集粘贴帖子。当我实际上有粘贴帖子时,这可以工作,但是当我没有粘贴帖子时,它只查询10个(无粘性)帖子。我如何调整查询以收集只有粘性的帖子,如果没有找到,只是什么都不做。感谢

/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );

/* Sort the stickies with the newest ones at the top */
rsort( $sticky );

/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 5 );

/* Query sticky posts */
$the_query2 = new WP_Query( array( 'ignore_sticky_posts' => 'false', 'post__in' => $sticky, 'post_type' => 'ad_listing' ) );
echo $the_query2->post_count;

1 个答案:

答案 0 :(得分:1)

通过添加此if语句,发现只查询粘贴帖子!

/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );

    if (!empty($sticky)) {
    //Do the query etc.
    }