Query Custom Fields using meta query
-
I can’t figure out how to query posts using meta_query when the custom field has been entered using the XYDAC fields interface.
I have a custom field ‘sticky’ which is a checkbox with label ‘Yes’. Someone would check it to ‘stick’ the post to the home page. I’m developing a widget to pull the sticky posts, but I never get any posts. Here is what I was trying:
$args = array ( 'post_type' => 'events', 'numberposts' => 10, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => array( array ( 'key' => 'sticky', 'value' => 'Yes', 'compare' => '=', ), ), ); $this->posts = get_posts( $args );
In the database, when the box is checked, the meta value looks like this:
a:1:{s:8:"checkbox";a:2:{s:3:"Yes";s:3:"Yes";s:10:"xydac-null";s:10:"xydac-null";}}
As a test, I created a custom field ‘test’ using the normal wordpress interface for this post type and assigned the value ‘Yes’ and was able to successfully query it using the same query above.
-
Hah. I decided to try this query instead and it worked. Is there an easier way?
$args = array ( 'post_type' => 'events', 'numberposts' => 10, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => array( array ( 'key' => 'sticky', 'value' => 'a:1:{s:8:"checkbox";a:2:{s:3:"Yes";s:3:"Yes";s:10:"xydac-null";s:10:"xydac-null";}}', 'compare' => '=', ), ), ); $this->posts = get_posts( $args );
Looks like this may not be possible to accomplish.
http://wordpress.stackexchange.com/questions/16709/meta-query-with-meta-values-as-serialize-arrays
Can you think of other ways? I’m trying to avoid pulling more than I need and doing the filter in PHP if I can help it.
Hie,,
I have seen people having various issues using the serialized values in meta data. I have decided to revert them back to normal form which i’ll be doing in next release.Hope you can wait till then..:)
That is going to be AWESOME! I can absolutely wait. If you want any help, just let me know what I can do. I’m a PHP/Wordpress guru and I’m good about following another programmer’s coding conventions.
- The topic ‘Query Custom Fields using meta query’ is closed to new replies.