I'm using Get Posts plugin to list posts with post type "project". I want to filter the list by two custom fields: year (ex. 2006) and state (ex. Completed). I added meta_query to the plugin's get_post args and tried the shortcode:

[get_posts post_type="project" meta_query="array(array('key' => 'state', 'value' => 
'Completed'),array('key' => 'year','value' => '2006'))" suppress_filters="false"]

This is not working, though I can get both "state" and "year" to work by their own using meta_key and meta_value.

Can someone put me in the right direction?


Besides the plugin not being updated, this will not work because the meta_query arg is evaluated as a string:

array(3) {
  ["post_type"] => string(7) "project"
  ["meta_query"] => string(96) "array(array('key' => 'state', 'value' => 
'Completed'),array('key' => 'year','value' => '2006'))"
  ["suppress_filters"] => string(5) "false"

I suggest you make a custom page template, where you would directly call get_posts() with the meta_query you want.

You will have to copy the code that handles the actual listing of the posts.

| improve this answer | |

I don't think the plugin been updated to support the new query args, or specifically in this case, the meta_query arg.

The plugin page lists compatibility upto 3.0.5, and if i remember correctly, meta_query was introduced in 3.1, so naturally i'd conclude support is not yet available..

I could be wrong, just connecting the dots..

| improve this answer | |

The plugin uses shortcode_atts to limit the list of accepted arguments:


meta_query is not on that list. I'd suggest forking the plugin or asking the author to update it to allow meta queries.

| improve this answer | |
  • Thank you. So, if get_posts() does not support meta_query, i'll need a WP_Query() or query_posts() shortcode plugin of some sort? Will they behave properly as a shortcode within a page, i.e. not in a template? Am I asking the right questions? What is, after all, the right way to get a filtered list by two custom fields? – AnaRita Apr 18 '11 at 14:39
  • get_posts() is just a wrapper for WP_Query, so meta_query does work, as well as any other arg that WP_Query can handle. – scribu Apr 18 '11 at 14:41
  • The second part about get_posts() not accepting meta_query is not correct, but the first part about it filtering attributes via shortcode_atts() is. I modified this answer to reflect this, but I leave it up to @John to do a complete edit. – Jan Fabry Apr 18 '11 at 15:42
  • Yeah, my mistake. I was mistaken about how wp_parse_args() worked. – John P Bloch Apr 18 '11 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.