最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Use not custom fields in get_posts() meta_query?

programmeradmin2浏览0评论

Is it possible to use not custom fields in get_posts()'s 'meta_query' to use a or-relation statement with a custom field and a category id?

Like this:

$args = array(
        'posts_per_page' => 6
      , 'offset'         => 0
      , 'meta_query'     => array(
                                 'relation' => 'OR'
                               , array(
                                      'key'   => 'placing'
                                    , 'value' => 'nn'
                                 )
                               , array(
                                      'key'   => 'category_id'
                                    , 'value' => $cat->term_id
                                 )
                            )
    );

$myposts = get_posts($args);

And if so, should it be term_id or category_id?

Or how do I accomplish that?

Is it possible to use not custom fields in get_posts()'s 'meta_query' to use a or-relation statement with a custom field and a category id?

Like this:

$args = array(
        'posts_per_page' => 6
      , 'offset'         => 0
      , 'meta_query'     => array(
                                 'relation' => 'OR'
                               , array(
                                      'key'   => 'placing'
                                    , 'value' => 'nn'
                                 )
                               , array(
                                      'key'   => 'category_id'
                                    , 'value' => $cat->term_id
                                 )
                            )
    );

$myposts = get_posts($args);

And if so, should it be term_id or category_id?

Or how do I accomplish that?

Share Improve this question asked Sep 25, 2015 at 13:02 Peter WesterlundPeter Westerlund 1,0775 gold badges14 silver badges31 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

No, it doesn't work like that because meta_query is for custom fields and tax_query is for taxonomies.

But what you could do is run two separate queries to get all of the IDs that qualify, and then use those IDs in a 3rd query with post__in. For example:

$cat_query_args = array(
    'posts_per_page' => -1,
    'cat'            => '63',
);

$meta_query_args = array(
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'placing',
            'value' => 'nn',
        )
    )
);

$cat_query  = get_posts( $cat_query_args  );
$meta_query = get_posts( $meta_query_args );

$merged_posts = array_merge( $cat_query, $meta_query );

$combined_query_args = array(
    'posts_per_page' => 6,
    'offset'         => 0,
    'post__in'       => $merged_posts
);

$combined_posts = get_posts( $combined_query_args );
发布评论

评论列表(0)

  1. 暂无评论