使用缩略图ID作为元键检索帖子

时间:2016-02-07 作者:Piyush Rawat

我正在我的网站上工作,并在wp\\u查询中使用这些参数,但我也想只显示那些有缩略图的帖子。。。以下是我正在处理的参数:

        $args1 = array(
            \'posts_per_page\'        => $number,
            \'no_found_rows\'         => true,
            \'post_status\'           => \'publish\',
            \'offset\'                => $offset,
            \'ignore_sticky_posts\'   => true,
            \'orderby\'               => $sort_order,
            \'meta_key\'              => ( ($sort_order == \'meta_value_num\') ? \'mip_post_views_count\' : \'\' )
        );

        $args2  = array();
        if ($include_categories) {
            //$include_categories = explode(",", $include_categories);
            $args2 = array(
                \'cat\'      => $include_categories
            );
        } 
以下是我正在尝试的:

    $args1 = array(
            \'posts_per_page\'        => $number,
            \'no_found_rows\'         => true,
            \'post_status\'           => \'publish\',
            \'offset\'                => $offset,
            \'ignore_sticky_posts\'   => true,
            \'orderby\'               => $sort_order,
            \'meta_key\'              => ( ($sort_order == \'meta_value_num\') ? \'mip_post_views_count\' : \'\' ),
            // this is what is added
            \'meta_query\'    => \'_thumnail_id\'
        );

        $args2  = array();
        if ($include_categories) {
            //$include_categories = explode(",", $include_categories);
            $args2 = array(
                \'cat\'      => $include_categories
            );
        }
但它改变了帖子的顺序。。。有人能提出解决方案吗?

1 个回复
最合适的回答,由SO网友:majick 整理而成

尝试为设置数组meta_query 相反,我不认为只使用字符串是预期用途,但在您的情况下,这可能会起作用:

\'meta_query\' => array(
    array(
        \'key\'     => \'_thumbnail_id\',
        \'compare\' => \'EXISTS\'
    )
)