Custom loop using a meta_value that is an array
-
Hi All,
I am currently trying to output one random entry from a custom post type. The post type has a custom field called ‘person_type’, the values in this field are an array, e.g. array(0=>’advisor’, 1=>’contributor’)
I am trying to set up a custom loop that pulls advisors only. The code I am using is:
$args = array( 'post_type' => $type, 'orderby' => 'rand', 'meta_key' => 'person_type', 'meta_value' => 'advisor' ); } $loop = new WP_Query($args);This doesn’t return anything.
I have tried changing the meta_value to:
'meta_value' => array('advisor'),however this throws up an error saying:
Warning: trim() expects parameter 1 to be string, array given in /wp-includes/meta.php on line 526
In other words it expects a string, any help would be appreciated!
Thanks,
Scoe
-
I changed the way it worked and put each of the array items into their own custom field.
The $args are now:
$args = array( 'post_type' => $type, 'orderby' => 'rand', 'meta_key' => 'person_advisor', 'meta_value' => true ); } $loop = new WP_Query($args);Thanks anyway
Hey, I’m having the same problem, but I just walked into the project close to its finish. I can’t really change their data structure at this point. Here’s the code:
$args = array( 'numberposts' => -1, 'post_type' => 'post', 'meta_query' => array ( array ( 'key' => 'my_key', 'value' => 'target_value', ) ) ); $new_query = new WP_Query( $args );The key ‘my_key’ always has an array as its value, so of course it will never match the queried ‘target_value’. How do I find the ‘target_value’ in the array? This is an array of strings by the way.
Any help would be really appreciated!!
Thanks. -JohnPlease post a new topic.
The topic ‘Custom loop using a meta_value that is an array’ is closed to new replies.