Support

Account

Home Forums Backend Issues (wp-admin) WP query value array

Solved

WP query value array

  • Hello!

    I have the following wp_query

    $posts = get_posts(array(
        'post_type'     => 'product',
        'post_status'   => array('publish', 'draft'),
        'orderby'       => 'publish_date',
        'order'         => 'DESC',
        'posts_per_page'=> 20,
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'category',
                'value' => 'cat_a',
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'category',
                'value' => 'cat_b',
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'category',
                'value' => 'cat_c',
                'compare' => 'LIKE'
            ),
        )
    ));

    Is there any way how to write the meta_values as an array rather than listing all the meta_query values possibilities?

    What I want is a query that displays every post that has the category “cat_a” OR “cat_b” OR “cat_c”. Something like:

    array(
        'key' => 'category',
        'value' => array('cat_a','cat_b','cat_c'),
        'compare' => 'LIKE'
    ),
  • This reply has been marked as private.
  • No, unfortunately there isn’t, not using an ACF select field.

    If it is a standard WP field then yes, you could us the “IN” compare type in your query. That would require conversion of the ACF select field to standard meta field storage. I wrote an article about this, it is about repeaters, but the same principle can be applied to select fields https://acfextras.com/dont-query-repeaters/

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘WP query value array’ is closed to new replies.