0

I've been looking at this old theme. I've then created a template file called taxonomy template file and post types. When querying to show only the posts in the category selected. This is the top of my template :

    $args=array ( 

            'post_type'=> array( 'events' ),
            'tax_query' => array(  'taxonomy' => 'tax-cat' ), 
            'post_status' => array( 'future' ), 
            'pagination' => true, 
            'paged' => $paged,
            'orderby' => 'post_date',
            'order' => 'ASC', ); 
    $temp = $wp_query; 

    $wp_query = null; 
    $wp_query = new WP_Query(); 
    $wp_query->query( $args ); 

Which ive taken from the existing page template, but the above seems to show ALL Posts in the custom post type, rather than just the posts in the category.

  • Every taxonomy has its own archive and URL, have you tried using taxonomy-eventcat.php and visiting an eventcat terms archive? This would eliminate the need to create a query entirely. You can then modify the query to show the future post type via pre_get_posts, and you'd also get a speed boost as you'd no longer be replacing the main query but modifying it instead, and all your pagination code could be removed as it's no longer needed – Tom J Nowell Oct 4 '17 at 13:25
1

The problem is not that you never specify which event cat in your query, but that you're doing a query at all.

If you want to change what the main query pulls in, change the query, don't add a second query. We do this via the pre_get_posts filter:

This hook is called after the query variable object is created, but before the actual query is run.

The pre_get_posts action gives developers access to the $query object by reference (any changes you make to $query are made directly to the original object - no return value is necessary).

For example:

function my_home_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '123' );
    }
}
add_action( 'pre_get_posts', 'my_home_category' );

In your case, you want to use the filter:

  • only if it's the main query
  • only if it's an archive
  • and that archive is for the events category
  • to set the post status to future not publish
  • and change the ordering from DESC TO ASC

Since the current requested term etc are already set, you will need a check similar to this:

function randomer11_fix_eventcat_archive( \WP_Query $q ) {
    if ( $q->is_main_query() && $q->is_archive() && $q->is_tax( 'eventcat' ) ) {
        $q->set( ... ); // set your options
    }
}
add_action( 'pre_get_posts', 'randomer11_fix_eventcat_archive' );

You can call set as many times as you want, and now in your template, you can remove your query, and use a normal post loop. Pagination will work how its supposed to without any hacks, and your page will load a lot faster

  • Never mind, figured it out :) Thanks alot Tom. Learnt something new :) – Randomer11 Oct 4 '17 at 15:25

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.