1

I know that this question is asked numerous but i din't find an answer that suits to me.

Well i'm made some custom post types and some custom taxonomies. I have the custom post type Products and linked to it the taxonomy Product Categories. When the user is at the Products page and clicks on a Product Category, i want to show him all the products from this specific category.

The problem is that the product categories will be about 50. Right now the only thing that i found is to put to the wordpress loop this

<?php $loop = new WP_Query( array( 'post_type' => 'all_products', 'posts_per_page' => 12, 'products' => 'advance' ) ); ?>

But this shows the posts only for the advance category. I need a way to automatically takes the category that the user clicked. The dirty way is to make 50 taxonomy-{slug}.php files. But i want the clean way. And i can't seem to find it.

I read all the codex about the template hierarchy and specific for the custom taxonomies, and search the half stackexchange but nothing (maybe i've missed something :P ).

I tried to describe my question as clear as i can. If you cant understand something just tell me and i'll try to update the question with more info.

Thank you

  • Use pre_get_posts to change your posts_per_page – Pieter Goosen Aug 6 '14 at 10:56
  • just this? and i should take out the 'products' => 'advance' ? – hambos22 Aug 6 '14 at 11:01
1

The way I read you question is that you need 12 posts per page in this taxonomy regardless of the current term been displayed.

This can be easily done with pre_get_posts. You should never change the main query for a custom query on archive pages. Have a read on this answer I've recently done on this subject.

Use the is_tax() conditional to target the taxonomy page and also see the parameters

Add this to functions.php. You can also only use one taxonomy.php for all terms, no need to make 50 others

function custom_ppp( $query ) {
    if ( !is_admin() && $query->is_tax() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', '12' );
    }
}
add_action( 'pre_get_posts', 'custom_ppp' );

EDIT

In taxonomy.php, change this

<?php $loop = new WP_Query( array( 'post_type' => 'all_products', 'pre_get_posts' => 12, 'products' => 'advance' ) ); ?> 

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?> 
    //content 
<?php endwhile; ?>

back to just this

<?php while ( have_posts() ) : the_post(); ?> 
   //content
<?php endwhile; ?>
  • Php noob here! I included your code to my theme's functions.php. What i need to modify right now in taxonomy.php? Right now is something like this <?php $loop = new WP_Query( array( 'post_type' => 'all_products', 'pre_get_posts' => 12, 'products' => 'advance' ) ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post(); ?> //content <?php endwhile; ?> – hambos22 Aug 6 '14 at 11:32
  • Change it back to the main loop as it originally was – Pieter Goosen Aug 6 '14 at 11:34
  • My pleasure, glad you solved your problem – Pieter Goosen Aug 6 '14 at 11:45
0

What you are describing is loosely related to "faceted search", but you aren't quite searching. :)

In a nutshell you would need to:

  1. Communicate information about "selected" taxonomy to the page, this might be done with GET query or rewrite endpoint
  2. Generate links (or whatever interface you have) accordingly
  3. Adjust main query (!) of the page via pre_get_posts, changing queries in template you will very likely hit pagination issues
0

This is what i am using for Custom Post Type health and Custom Taxonomy health-category also fixes the pagination.

<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$q = new WP_Query([
    'post_type' => 'product',
    'posts_per_page' => get_option('posts_per_page'),
     get_query_var( 'taxonomy' ) => get_query_var( 'term' ),
    'paged' => $paged
]);
$temp_query = $wp_query;
$wp_query = null;
$wp_query = $q;

if ( $q->have_posts() ) :
    while ( $q->have_posts() ) : $q->the_post();

        // post content goes here..

    endwhile;
endif;

// Reset postdata
wp_reset_postdata();

// pagination goes here..

// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.