WordPress多查询未按预期返回

时间:2013-10-22 作者:Jody Heavener

我正在尝试执行一个合并查询,以从post 职位类型和我的events 自定义帖子类型。这个post 查询基于是否设置了特征图像,以及events 查询基于帖子是否选择了元值。一切正常,除了:The query only returns 5 posts, and they are not balanced in the sense that [depending on which post I set as Featured/Add Featured Image too] they take precedence over one another.

例如:如果我向post post结果将是3个事件,2个事件。如果我在events post类型结果将是4个事件,1个post。

以下是我用来查询和返回帖子的代码:

        <?php
        $post_types = get_post_types(\'\',\'names\');
        $featuredEvents = get_posts(array(
            \'post_type\' => \'events\',
            \'post_status\' => \'publish\',
            \'showposts\' => 6,
            \'meta_key\' => \'_ecmb_featured_event\',
            \'meta_value\' => \'on\'
        ));
        $featuredNews = get_posts(array(
            \'post_type\' => \'post\',
            \'post_status\' => \'publish\',
            \'showposts\' => 6,
            \'meta_key\' => \'_thumbnail_id\'
        ));
        $mergedPosts = array_merge( $featuredEvents, $featuredNews );
        $postIDs = array();
            foreach( $mergedPosts as $item ) {
            $postIDs[] = $item->ID;
        }
        $uniquePosts = array_unique($postIDs);

        $posts = get_posts(array(
            \'post__in\' => $uniquePosts,
            \'post_type\' => $post_types,
            \'post_status\' => \'publish\',
        ));

        foreach( $posts as $post ) :
        setup_postdata($post);
        if( get_post_type() == \'events\' ) { ?>
                    This is a Featured Event
        <?php } elseif (get_post_type() == \'post\') { ?>
                    This a post with the Featured Image set
        <?php } endforeach; wp_reset_query(); ?>
有人能帮我吗?

Edit:

由于下面的答案,我找到了答案,并使用了以下代码:

$sliderQueryArgs = array(
    \'posts_per_page\' => 6,
    \'post_type\' => array(
        \'post\',
        \'events\'
    ),
    \'orderby\' => \'post_date\',
    \'meta_query\' => array(
        \'relation\' => \'OR\',
            array(
                \'key\' => \'_ecmb_featured_event\',
                \'value\' => \'on\'),
            array(
                \'key\' => \'_thumbnail_id\',
                \'compare\' => \'EXISTS\')
    )
);

$sliderPosts = get_posts( $sliderQueryArgs );

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

你错过了一个事实get_posts() (或WP_Query) 可以采取array() 单数查询中的帖子类型。More info on Type Parameters in the Codex. 默认情况下,订单将为发布日期。

然后,您可以通过添加诸如taxonomy_query, 等

记住你可以orderby 通过meta_value 以及分配多个orderby 参数空格分隔:

\'orderby\' => \'meta_value post_date\'
例如。

结束

相关推荐

PHP警告:Call_User_Func()要求参数1是有效的回调

困惑的编写一个插件,其他回调也可以很好地使用它。但添加元框时,回调失败,出现错误:PHP警告:call\\u user\\u func()要求参数1为有效回调,第一个数组成员不是有效的类名或对象相当标准的错误,但我不明白为什么我会收到它。我在一节课上有以下内容。function __construct(){ add_action( \'init\', array( &$this, \'init\' ) ); } function init() {&