WordPress Custom Content and Taxonomy’s Archives

Ok so I have set up a custom content type: Event

I have then set up the custom taxonomies in hierarchical format for Event: Type

Read More

To display “event” posts in each “type”, I’m using the template: taxonomy-type.php

I have then set up a widget “Custom Taxonomies Menu Widget” to list the “types” in one of my widget areas with a count for post in each type.

My issues:

Clicking one of them takes me to pages that don’t match the “event” posts that should be there for that “type”. (one of which, the party “type, shows no posts available even though I have some in that “type”.

The count is not right for the posts in each category. (not that big of a deal compared)

Related posts

Leave a Reply

1 comment

  1. Firstly, the ‘future’ status is for posts that are not yet published and should not be used to mark events that occur in the future.

    (I would recommend storing the event’s data in the post meta, and then query/order by that post meta field. See this question and this question)

    Secondly, in your code you have these two lines:

    $args = array( 'post_type' => 'event', 'posts_per_page' => 50, 'post_status' => 'future', 'order' => 'ASC' );
    $loop = new WP_Query( $args );
    

    These completely override the default WordPress query which attempts to find ‘published’ events from your database that match the taxonomy term. (keep in mind ‘future’ events/posts are not considered ‘published’.)

    Your $loop query attempts to query the database for any ‘event’ that is not yet published, and since this over-rides the original query, it should show the same for every taxonomy term.

    But you don’t see this… (sometimes you see that there are no events). This is because of the conditional:

    have_posts();
    

    This is checking if the orginal query had any posts (i.e. are there any published events in this taxonomy term). It is not checking your current query (are there any future events?). To check your $loop query:

    $loop->have_posts();
    

    How to merge rather than replace the query

    So, above I’ve said that you are over-riding the query that WordPress automatically does (looks for published events for that term). To include the taxonomy-term request in your new query, use the following trick:

    Replace the two lines of your query with:

    $args = array( 'post_type' => 'event', 'posts_per_page' => 50, 'post_status' => 'future', 'order' => 'ASC' );
    //Merge with default query
    global $wp_query;
    $args = array_merge( $wp_query->query,$args);
    $loop = new WP_Query( $args );