Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
Thank you. I moved to the template solution. I'm now struggling with it over here: wordpress.stackexchange.com/questions/15114/… –  AnaRita Apr 19 '11 at 13:56
add comment

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

http://plugins.trac.wordpress.org/browser/nurelm-get-posts/tags/0.3/get-posts.php#L29

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

share|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
add comment

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..

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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