pre_get_posts and tax_query doesn’t work
-
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)
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.