Support

Account

Home Forums General Issues Search using custom post type and advanced custom fields through taxonomies

Helping

Search using custom post type and advanced custom fields through taxonomies

  • Hi,

    I have a custom post type named Houses.
    For this CPT, I have created some taxonomies to have a relationship with it:

    – Country (taxonomy)
    – City (taxonomy)
    – Beach (taxonomy)
    – Condominium (post object)

    How can I search a term and look into these relations?

    For example,

    – if I search Los Angeles, it will bring all the houses which has a city called Los Angeles.
    – if I search a term which is common in a beach and city, it will bring all the houses related to this search

    Since it’s a taxonomy, how to do a search in all the advanced custom fields connected to my custom post type Houses?

    Thanks!

  • You have to add this to your query:

    So if you were trying to look for houses with the following criteria:
    CPT: houses
    Country: United States
    City: Los Angeles
    Beach: Manhattan

    I don’t know what your taxonomy slugs are, but insert them own below:

    $args = array(
    	'post_type' => 'houses',
    	'tax_query' => array(
    		'relation' => 'AND',
    		array(
    			'taxonomy' => 'country',
    			'field'    => 'name',
    			'terms'    => 'United States',
    		),
    		array(
    			'taxonomy' => 'city',
    			'field'    => 'name',
    			'terms'    => 'Los Angeles',
    		),
    		array(
    			'taxonomy' => 'beach',
    			'field'    => 'name',
    			'terms'    => 'Manhattan',
    		),
    	),
    );
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Search using custom post type and advanced custom fields through taxonomies’ is closed to new replies.