admin管理员组文章数量:1417426
I am doing a WP_Query
trying to filter out the posts by Metadata.
$args = array(
'ignore_sticky_posts'=> 1,
'post_type' => 'post_projects',
'post_status' => 'publish',
'posts_per_page' => $nr_posts,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'wpl_goal_amount',
'value' => 100,
'compare' => '!=',
),
array(
'key' => 'wpl_location',
'value' => 'Europe',
'compare' => '=',
),
),
);
I am testing this out with two posts. One of them has "Europe" in it's array of locations, but whenever I run this, it doesn't come up with anything.
QUESTION UPDATE:
I am using Option Tree to save the Meta-data, and as mentioned in a previous answer, it stores serialized the post meta-data (by Wordpress' default), and in combination with that and another post, I found that there are two ways to query the serialized data. The first is more convenient, but more of a jerry-rig, and the second is more proper, but difficult:
The easier:
array( 'key' => 'wpl_location', 'value' => '"' . $my_value . '"', 'compare' => 'LIKE' ),
Compared to the better but more difficult:
array( 'key' => 'wpl_location', 'value' => serialize(array(3 => 'Europe')), 'compare' => 'LIKE' ),
I believe the second method would require me to have a (near) identical serialized array as the database, which would not work in the long-term for the system that I am creating, because it will need to match one out of many items in the array.
My updated question:
Should I continue down this path and use the first method, or do I need to dump Option Tree and figure out a way to add the meta-data to the posts in unserialized form so that it can be queried?
I am doing a WP_Query
trying to filter out the posts by Metadata.
$args = array(
'ignore_sticky_posts'=> 1,
'post_type' => 'post_projects',
'post_status' => 'publish',
'posts_per_page' => $nr_posts,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'wpl_goal_amount',
'value' => 100,
'compare' => '!=',
),
array(
'key' => 'wpl_location',
'value' => 'Europe',
'compare' => '=',
),
),
);
I am testing this out with two posts. One of them has "Europe" in it's array of locations, but whenever I run this, it doesn't come up with anything.
QUESTION UPDATE:
I am using Option Tree to save the Meta-data, and as mentioned in a previous answer, it stores serialized the post meta-data (by Wordpress' default), and in combination with that and another post, I found that there are two ways to query the serialized data. The first is more convenient, but more of a jerry-rig, and the second is more proper, but difficult:
The easier:
array( 'key' => 'wpl_location', 'value' => '"' . $my_value . '"', 'compare' => 'LIKE' ),
Compared to the better but more difficult:
array( 'key' => 'wpl_location', 'value' => serialize(array(3 => 'Europe')), 'compare' => 'LIKE' ),
I believe the second method would require me to have a (near) identical serialized array as the database, which would not work in the long-term for the system that I am creating, because it will need to match one out of many items in the array.
My updated question:
Should I continue down this path and use the first method, or do I need to dump Option Tree and figure out a way to add the meta-data to the posts in unserialized form so that it can be queried?
Share Improve this question edited Aug 7, 2017 at 6:03 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Jul 9, 2015 at 15:25 lawdawglawdawg 518 bronze badges2 Answers
Reset to default 0You might be missing the 'meta_key' arg here:
$args = array(
'ignore_sticky_posts'=> 1,
'meta_key' => 'wpl_location', // here
'post_type' => 'post_projects',
'meta_query' => array(
array(
'key' => 'wpl_location',
'value' => 'Europe',
'compare' => 'IN',
),
),
);
source: https://codex.wordpress/Class_Reference/WP_Query (about three quarters down the page).
If you're saving an array into Metadata it becomes Serialized. This makes it pretty difficult to query on, maybe try using wildcard:
$args = array(
'ignore_sticky_posts'=> 1,
'post_type' => 'post_projects',
'meta_query' => array(
array(
'key' => 'wpl_location',
'value' => '%Europe%',
'compare' => 'LIKE',
),
),
);
This will search the serialized data for anything that looks like "Europe".
本文标签: wp queryWPQuery Posts by Metadata from Option Tree
版权声明:本文标题:wp query - WP_Query Posts by Metadata from Option Tree 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745270262a2650847.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论