Show only posts with a specific taxonomy term in taxonomy archive template
-
I have set up a custom post type called Prototype Games on my website. It has a long list of taxonomies associated with it, and each of the taxonomies has between 3-40 terms.
The first taxonomy I set up an archive template for is called event with terms like “July 2021” and “April 2021,” which represent dates for our online convention.
I was able to get the the archive page for this taxonomy working somewhat as expected. It does show all post with the “July 2021” term applied, but it also includes posts with “April 2021” set as the term for the event taxonomy in the archive listing at https://protospiel.online/event/july-2021/
SIDENOTE: I’ve also set up a custom permalink slug for %event% with this code block in my functions.php file:
function event_permalink($custom_permalink, $post_id, $leavename) { if (strpos($custom_permalink, '%event%') === FALSE) return $custom_permalink; // Get post $post = get_post($post_id); if (!$post) return $custom_permalink; // Get taxonomy terms $terms = wp_get_object_terms($post->ID, 'event'); if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug; else $taxonomy_slug = 'no-event'; return str_replace('%event%', $taxonomy_slug, $custom_permalink); }
I’ve got the pagination at the bottom of the event archive template working exactly as expected.
After setting this all up, I moved on to the next taxonomy, Components. It has terms in it like “(Actual) Meeples” “Miniatures” and “Tiles” — different pieces people might use in their prototype games.
I set all of the code from the first template up as an include and called it into my two theme files, taxonomy-event.php and taxonomy-components.php, using the code:
get_template_part('includes/prototype-taxonomy-archive');
As with the event taxonomy, the archive page for the component taxonomy is listing a bunch of posts that don’t have the component in question applied to them.
Here’s the example for the (Actual) Meeples term: https://protospiel.online/components/actual-meeples/
When a component term has been applied to a post, that term shows up as linked text in the conditionally visible “Components” section near the bottom of the page on the single post template.
Here’s an example of a post that has the (Actual) Meeples term:
https://protospiel.online/prototype-games/july-2021/all-ctp-fields-populated-entry-751/And here’s one that doesn’t have it:
https://protospiel.online/prototype-games/july-2021/designer-tax-test-entry-745/Both of these post show up in the list on the (Actual) Meeples archive page.
In addition to this issue, clicking the “Next” link at the bottom of the archive page brings up a 404 error.
One thing that is different between these 2 taxonomies is that ‘event’ is more like a category — it’s a primary aspect of the post, and there should only ever be one event applied to a post (there isn’t anything physically stopping extras from getting applied, though — we’ll just never do it in practice). ‘Components’ is more like a tag — one post can have lots of them applied to it, as you can see in the “all-ctp-fields-populated-entry-751” example above.
I am using a Genesis child theme. This is how the loop on my archive template begins:
remove_action ('genesis_loop', 'genesis_do_loop'); // Remove the standard loop add_action( 'genesis_loop', 'custom_do_grid_loop' ); // Add custom loop function custom_do_grid_loop() { $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $tax=get_query_var('taxonomy'); $tax_term=get_query_var('term'); $args = array( 'post_type' => 'prototype-games', // custom post type name 'orderby' => 'date', 'order' => 'DSC', 'paged'=> $paged, 'tax_query'=> //added this recently, but it didn't help or hurt the situation array( 'taxonomy'=>$tax, 'field'=>'slug', 'terms'=>$tax_term, ), ); $loop = new WP_Query( $args ); if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post(); global $post;
And this is how it ends (including the pagination code)
endwhile; echo '<div class="row med-margin-top text-center"><strong>'; echo previous_posts_link( __( '« Previous', 'textdomain' ) ) .' | '; echo next_posts_link( __( 'Next »', 'textdomain' ), $loop->max_num_pages ) . '</strong>'; echo ' </div>'; endif; // Clean up after the query and pagination. wp_reset_postdata(); } // Runs the Genesis loop. genesis();
What am I doing wrong? Any help is very much appreciated!
The page I need help with: [log in to see the link]
- The topic ‘Show only posts with a specific taxonomy term in taxonomy archive template’ is closed to new replies.