• petegale

    (@petegale)


    Hi there,

    I’ve been having a lot of trouble getting a WP_Query running with a tax_query on my custom taxonomy.

    I’m 99.9% sure that my register_taxonomy is correct, as I’m able to tag posts with the right term, see it in the database, and the correct term returns with an echo custom_taxonomies_terms_links();.

    But when I use a tax_query in my WP_Query, I get no posts. If I look at the echo of $GLOBALS['nextSundayTalkQuery']->request;, I get the following:

    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
    FROM wp_posts WHERE 1=1
    AND 0 = 1
    AND wp_posts.post_type = 'talk'
    AND (
        wp_posts.post_status = 'publish'
        OR wp_posts.post_author = 1
        AND wp_posts.post_status = 'private'
    )
    GROUP BY wp_posts.ID
    ORDER BY wp_posts.post_date DESC
    LIMIT 0, 1

    I’ve tried no end of solutions, asked for help in several places, and have had no luck. I’m posting again for help with slightly updated information, so hopefully this time I might find an answer.

    My WP_Query looks like this:

    $nextSundayTalkArgs = array(
    	'post_type' => 'talk',
    	'posts_per_page' => 1,
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'talktype',
    			'field' => 'slug',
    			'terms' => 'sunday-talk'
    		)
    	)
    );
    $nextSundayTalkQuery = new WP_Query( $nextSundayTalkArgs );

    It works perfectly without ‘tax_query’. If I use something like 'talktype' => 'sunday-talk' instead, using query_var when I register the taxonomy, it simply ignores the line as thought it weren’t there and prints any talk (instead of saying “no posts”).

    Using identical code to query WordPress’ default “category” taxonomy works fine, so it seems to be related to my custom taxonomy or post type. To save space on this post, my custom post type code is here:

    https://pastebin.com/LxKt2pv5

    and my custom taxonomy code is here:

    https://pastebin.com/NxuuxKuG

    I would appreciate any and all help, as this problem has been unsolvable for months with many people trying (and failing, unfortunately) to figure out what’s wrong.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter petegale

    (@petegale)

    I mention that custom_taxonomies_terms_links() returns the right terms, but forgot to include that function:

    https://pastebin.com/18Aj1ysT

    niaccurshi

    (@niaccurshi)

    To me the problem looks to be the line that say

    AND 0 = 1.

    Naturally that’s going to give you no results, since it’ll always be false.

    A quick search discovers https://stackoverflow.com/questions/11148983/using-tax-query-creates-a-0-1-in-wp-query-request

    Which suggests there may be a need to do something a little more intelligent to discover children posts in the right way.

    I wonder if this will sort you out? https://tysonarmstrong.com/using-tax_query-with-hierarchical-custom-taxonomies/

    Thread Starter petegale

    (@petegale)

    Thanks for replying ??

    I had tried “include_children” as false before, but the solution you linked seems a little more in depth so I gave it a go.

    I’m setting up my query slightly differently to the example, so I changed mine to the following which should hopefully incorporate the proposed solution:

    $nextSundayTalkArgs = array(
    	'post_type' => 'talk',
    	'posts_per_page' => 1,
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'talktype',
    			'field' => 'slug',
    			'terms' => 'sunday-talk',
    			'operator' => 'IN'
    		),
    		array(
    			'taxonomy' => 'talktype',
    			'field' => 'slug',
    			'terms' => 'sunday-talk',
    			'operator' => 'IN',
    			'include_children' => 0
    		),
    		'relation' => 'OR'
    	)
    );
    $nextSundayTalkQuery = new WP_Query( $nextSundayTalkArgs );

    Unfortunately, no luck. I got the same SQL output as before, and no posts ?? If it looks like I haven’t tried the solution properly, please let me know – but it doesn’t seem to have done the trick.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Taxonomy and Tax_Query’ is closed to new replies.