Support » Fixing WordPress » Using new meta_query with custom post types

  • Resolved theryancollier

    (@theryancollier)


    I’m having trouble using the new meta_query argument with a custom post type (not sure if that even has anything to do with it). Basically I’m trying to use a custom field value to sort through my custom post type.

    function my_function($branch){
           $args = array(
    		'posts_per_page' => 10,
    		'post_type' => 'officer_profile',
    		'meta_query' => array(
    			'key' => 'asg_branch_gvt',
    			'value' => $branch
    			)
    		);
    	$officers = new WP_Query($args);
    }

    But when I use the older method and remove the meta_query array and replace it with meta_key => 'asg_branch_gvt', meta_value => $branch

    It returns no problem.

    What gives? Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Of course, right after I post I figure out the problem. You actually need to pass and array of arrays. So my code works now that I changed the code to:

    function my_function($branch){
           $args = array(
    		'posts_per_page' => 10,
    		'post_type' => 'officer_profile',
    		'meta_query' => array(
                            array(
    			'key' => 'asg_branch_gvt',
    			'value' => $branch
    			))
    		);
    	$officers = new WP_Query($args);
    }

    Luckily I wasn’t the only one with this problem in the last few hours. 😉 See http://core.trac.wordpress.org/ticket/16563 gives details.

    I think more than anything, some improvement to the example usage in the query_posts Codex page would help. See the heading “Multiple Custom Field Handling” at http://codex.wordpress.org/Function_Reference/query_posts. In fact that code example may be wrong.

    Weird, this didn’t work for me.

    Rather, this worked as I was working with query_posts.

    $args["meta_query"] = array("state"=>  array("Delhi", "IN"));
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using new meta_query with custom post types’ is closed to new replies.