admin管理员组文章数量:1319467
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
本文标签: queryHow can I get posts by 2 meta keysprioritising one of them
版权声明:本文标题:query - How can I get posts by 2 meta keys, prioritising one of them? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742062428a2418647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论