0

Background: On my site, each product's post has two sets of the same features. One is set by a program that imports products, the other is set by admin. The features can be price, weight, brand, a media file ID, etc. (either number or string). This makes sure the program just can't overwrite data inserted manually and the admin can restore the program's data at any time. Template files just get the two sets of data and replace program-imported features with the ones set by admin.

In my database, meta can look like this:

  • key:'myprefix_brand_program', value:'somename'
  • key:'myprefix_brand_site', value:'noname'

(may be program only, site only, or both)

Next I need to get my get_posts meta query to work with this setup.

I know how to write multiple media queries in WordPress:

$search = 'noname';
$args = array(
    'post_type' => 'myposttype',
    'numberposts' => -1,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'myprefix_brand_program',
            'value' => $search,
            'compare' => '='
        ),
        array(
            'key' => 'myprefix_brand_site',
            'value' => $search,
            'compare' => '='
        )
    )
);
$posts = get_posts($args);

This will get posts if either of the two keys has the matching value. What I need is to get posts by site-set value; if it hasn't been set, then by the program-set value. Is there a WordPress way to do it, without making two requests and comparing arrays?


If I am looking for brand 'somename', what I'm looking for is:

  • brand_site exists, 'brand_site == somename' is false, 'brand_program == somename' is true: do not get such posts
  • 'brand_site == somename' is true: do get such posts
  • 'brand_site' does not exist, 'brand_program == somename' is false: do not get such posts
  • 'brand_site' does not exist, 'brand_program == somename' is true: do get such posts
  • both does not exist: do not get such posts
  • I think your query is already doing that.. But you'd want to reorder the meta query clauses, i.e. Put the myprefix_brand_site clause above the other clause. – Sally CJ Oct 14 '20 at 11:29
  • What if I am looking for a 'somename' brand while for a post it's 'noname' on site and 'somename' in program? Logically, as I understand the 'OR' relation, it still must return the post. Am I wrong? I want my query not to return the post in this case. – tolkodelo Oct 14 '20 at 14:32
  • No, you're not wrong. Anyway, what you're trying to do is possible, you just need to use nested clauses. Have you tried doing so? – Sally CJ Oct 15 '20 at 19:10
  • @Sally CJ with every really good piece of advice, there's that feeling that you (almost) knew the answer yourself ))) thank you for pushing me in the right direction, I'll play with nested clauses. – tolkodelo Oct 16 '20 at 1:53

Your Answer

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

Browse other questions tagged or ask your own question.