Support

Account

Home Forums General Issues How to Query CPT by Relationship Field

Helping

How to Query CPT by Relationship Field

  • Hi All,

    I am trying to query a set of posts to display that are specific to a relationship set using the Relationship Field. I have read through a lot of posts and instructions and think that I am close but it’s still not working.

    My specific goal is to display certain Listings for the associated Agent. I will ultimaley display these listings in a map and list, but right now I just need to pull the Listings through a Query of some kind.

    Here is what I have:
    -I have these two post types linked through the Relationship field: Agent, Listing
    -My Relationship field is located on the Listing post type, and is set to return, Post ID
    -My code:

    $args = array(
    						'numberposts'	=> -1,
    						'post_type'		=> 'listing',
    						'meta_query' => array(
    							array(
    								'key' => 'your_agent',
    								'value' => '"' . $post->ID . '"',
    								'compare' => 'LIKE'
    							)
    						) 
    					);
    
    					$agentsListings = new WP_Query( $args );

    Any help would be greatly appreciated.

  • If I remove the meta_query portion of the $args (see below), I get all the post.

    
                                          $args = array(
    						'numberposts'	=> -1,
    						'post_type'		=> 'listing',
    					);
    
    					$agentsListings = new WP_Query( $args );
    

    But this won’t work because I need to pull the posts per agent (something like code below). I am doing some something wrong with the meta_query portion of my query. Please help!

    	$args = array(
    						'numberposts'	=> -1,
    						'post_type'		=> 'listing',
    						'meta_query' => array(
    							array(
    								'key' => 'your_agent',
    								'value' => '"' . $post->ID . '"',
    								'compare' => 'LIKE'
    							)
    						) 
    					);
    
    					$agentsListings = new WP_Query( $args );
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘How to Query CPT by Relationship Field’ is closed to new replies.