Support

Account

Home Forums General Issues Query a Relationship Field's Custom Fields

Helping

Query a Relationship Field's Custom Fields

  • Working on a Tour History catalog. I have venues, countries & live dates.

    ‘Live’ is a custom post type, which has a relationship field for Venues. On the Venues page it has a relationship field for Countries.

    On the countries page, I want to list all the ‘live’ posts, based on the value within the post in the relationship field of venues.

    Basically trying to query a key value within a key. Here’s what I have so far

    $shows = get_posts(array(
    	'post_type' => 'live',
    	'meta_query' => array(
    		array(
    			'key' => 'venue',
    				'meta_query' => array(
    					array(
    						'meta_key' => 'country',
    						'value' => '81',
    					)
    				)
    			)
    	)
    
    ));

    It’s not showing anything, or else it will just show all ‘live’ posts, regardless of venue or country.

    I would like to show only the ‘live’ posts which have any value for field ‘venue’ (relationship) but that ‘venue’ post has a specific country value. (in this example ID = 81).

  • You cannot query the fields of a related post. You would first need to query the other post type to return posts based it the values of its custom fields and then filter you second query using what is returned form that, for example with the “posts__in” argument.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.