Home > front end >  Wordpress filter by taxonomy ACF field
Wordpress filter by taxonomy ACF field

Time:10-29

I have a problem searching for posts for which I have created a special field in taxonomy.

$args = array(
        'posts_per_page' => '20',
        'paged'         => $paged,
        'post_type'     => 'cars',
        'order'   => 'DESC',
);

Taxonomy name: localization

With my cars post type I have a taxonomy relation, where there is a field I created called "city". How can I filter posts from "cars" post type by this custom field in taxonomy in wp_query?

I tried to write such tax_query, but I keep doing something wrong. Can you give me an example where someone filters it in a similar way by custom field?

CodePudding user response:

Your query would look something like:

$args = array(
    'post_type' => 'cars',
    'post_status' => 'publish',
    'posts_per_page' => 20,
    'paged' => $paged,
    'tax_query' => array(
        array(
            'taxonomy' => 'localization',
            'field'    => 'slug',
            'terms'    => array( 'tax1', 'tax2' )
        )
    )
);
$query = new WP_Query( $args );

CodePudding user response:

Hmm, but still not working. Look at this screen and let me know than code below is correct.

$args = array(
    'post_type' => 'cars',
    'post_status' => 'publish',
    'posts_per_page' => 20,
    'paged' => $paged,
    'tax_query' => array(
        array(
            'taxonomy' => 'localization',
            'field'    => 'slug',
            'terms'    => array( 'city')
        )
    )
);
$query = new WP_Query( $args );

enter image description here

enter image description here

  • Related