• Hello,

    I have a big issue on my theme.

    I create a theme and i have rewriting system like that (in regexp):

    • my_domain.fr/([^/]+)/?$ : display singular page (my_domain.fr/page1/)
    • my_domain.fr/custom_post_type/?$ : display custom_post_type archive page (my_domain.fr/projects/)
    • my_domain.fr/([^/]+)/cpt_archive_name/?$ : display custom_post_type archive page filter by term (my_domain.fr/web-design/projects)
    • my_domain.fr/([^/]+)/cpt_archive_name/([^/]+)/?$ : Display single custom post type page (my_domain.fr/web-design/projects/project1/).

    I try to alter the main loop but it’s doesn’t work, i don’t understand why. I use the pre_get_posts hook for that and i set the tax_query but nothing happened. Everyone have the same problem, i found multiple posts on Internet but no response.

    
    /**
     * Alter the main loop for project post-type (template : single-project.php)
     *
     * @param WP_Query $query
     * @return void
     */
    function single_project_alter_the_loop($query)
    {
        // Check  whether is for a single post query
        $is_single = is_single() && $query->is_single();
        // Get the post_type on query_var
        $post_type = $query->get('post_type', null);
        
        // Check whether the post_type matches
        if (('project' !== $post_type) || (!$is_single))
            return;
    
            // Check whether the term is passed in query_var
        if (null === ($term = $query->get('knowledge', null)))
            return;
    
        // Create array of param for alter the query
        $tax_query_value = array(
            array(
                'taxonomy'  => 'knowledge',
                'field'     => 'slug',
                'terms'     => $term,
            ),
        );
    
        // Put the tax_query
        $query->set('tax_query', $tax_query_value);
    }
    add_action('pre_get_posts', 'single_project_alter_the_loop');
    

    Save me please ! ^^

    Regards.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    What are you trying to accomplish with this? If the query already has the term for “knowledge” taxonomy set, why add the equivalent tax_query? I think the parser is getting confused with two ways to do the same thing. I don’t see the point of doing this at all, but if you are going to do so, unset the “knowledge” query var so only the tax_query is used to construct the taxonomy term clause in SQL.

    Thread Starter mamashi

    (@mamashi)

    In my project i customize the permalink of my cpt for include the term into the url like that :
    domain.com/{term_slug}/realisations/{cpt_name}
    In the loop the main query check this parameters by default : ?post_type={{cpt_post_type}&name={cpt_name}&knowledge={term_slug}
    In the single page template the url if the {term_slug} is wrong the page display still because the main query just ignore the term_slug and just check on the name and post_type value.
    I found while my reserach the tax_query doesn’t work for single page.
    I maybe a little lost.

    Moderator bcworkz

    (@bcworkz)

    When requesting a single post, all that really matters is the post title slug. By specifying other parameters, you risk causing a not found error even when the post tile slug is correct and the post exists. If the other parameters in fact match up with the requested single post, all should be fine, but we are still needlessly complicating the query.

    Unless it’s important in your situation to catch taxonomy mismatches and not return anything with single post requests, I think you are actually better off removing any taxonomy criteria at all when is_single() is true. It would remove a good bit of code from the parsing process and simplify the query itself so it runs more efficiently.

    AFAIK, assuming the tax_query terms are in fact assigned to a post, it should be compatible with single post queries. If the query fails to return the post, you should examine the actual SQL query found in $wp_query->request to find out why the query failed. Compare against the same query that used the “knowledge” query var instead of tax_query, assuming that one was working and tax_query was not.

    My observation still stands that I see no reason to convert a “knowledge” query_var to tax_query and that you need to remove the former if you choose to do so anyway.

    Thread Starter mamashi

    (@mamashi)

    I got it, I have already abandoned this idea. I just wanted to understand why it’s doesn’t work. I am using the ‘knowledge’
    query_varjust to pass the variable to my cpt archive page. Like a $_GET variable.
    Thank you for your time ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘pre_get_posts and tax_query doesn’t work’ is closed to new replies.