Support

Account

Home Forums Front-end Issues tax_query = no results

Solved

tax_query = no results

  • I’m really stumped as to why the following would not work:
    – custom post type “concert”
    – has custom taxonomy “concert_year” attached in ACF

    In my loop, I’m getting all published “concert” posts displayed:

    	$args = array(
    		'numberposts'	=> -1,
    		'post_type' => 'concert',
    		'post_status' => 'publish',
    	);
    $the_query = new WP_Query( $args );
    

    I can output the taxonomy term_id by having the_field('concert_year'); in the loop (it’s set to return the ID in ACF).

    This only works as long as I don’t add a taxonomy query in the args, however.

    	$args = array(
    		'numberposts'	=> -1,
    		'post_type' => 'concert',
    		'post_status' => 'publish',
    		'tax_query' => array(
    			array(
    				'taxonomy' => 'concert_year',
    				'field'    => 'term_id',
    				'terms'    => array( 26 ),
    			),
    		),
    	);
    $the_query = new WP_Query( $args );
    

    always has zero results, no matter if I use term_id or the slug.

    I’m sure there’s a ridiculously obvious error here, but frustratingly I can’t spot it.

  • Hi @philby

    Have you set the taxonomy field to map to the post? check the create terms, save terms and load terms settings in the field settings.

    Otherwise the taxonomies you select there will just be a meta value on the post. you could still query on it but it would be a meta_query.

  • Hi Jonathan

    the taxonomy field is indeed set to map to the post (if I understand it correctly): Create, Save and Load Terms are all set to Yes.

    The taxonomy is shared between this post type, three more custom pst types, and an options page, if that makes any difference.

    Thanks for any+all hints!

    -Phil

  • Okay.. when you say it’s shared.. do you mean that you’ve registered the taxonomy for the post type?

    Because I believe you have to register the taxonomy for the post type as well (it would make the native meta box appear). Otherwise ACF can’t mapp the terms selected.

  • Yes, the taxonomy is registered for all post types that use it:
    register_taxonomy( "year", array( "attachment", "concert", "interpret", "composer", "longread" ), $args );

    Could the error be in how the taxonomy is registered and what ACF uses as taxonomy name (“year” vs “concert_year”)?

  • Ah..

    Well that’s fine. the acf field can be called whatever..

    Your issue tho is that you’re using the ACF fieldname in your tax query for the taxonomy parameter when you should be using the taxonomy slug year.

    Change that and it should work.

  • Hi Jonathan

    thanks!
    I knew it would be something obvious.

  • No problem! Best of luck in your project.

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

You must be logged in to reply to this topic.