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