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?

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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.