WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using new meta_query with custom post types (3 posts)

  1. theryancollier
    Member
    Posted 3 years ago #

    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?

  2. theryancollier
    Member
    Posted 3 years ago #

    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.

  3. worldofharry
    Member
    Posted 2 years ago #

    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"));

Topic Closed

This topic has been closed to new replies.

About this Topic