• Resolved heatherdnewton

    (@heatherdnewton)


    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( __( '&laquo; Previous', 'textdomain' ) ) .' | ';
              echo next_posts_link( __( 'Next &raquo;', '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]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Really hard to say what you may be doing wrong, or may not be doing wrong in terms of the parts above, but are perhaps coming in from elsewhere.

    Probably the biggest thing I’d check, at least in terms of the cases of things like “Kings Cross – Entry #795” showing up incorrectly, would be what’s called the “Main Query” and then the actual request being made to the database.

    For example, for one of my personal sites, I have a pretty basic “books” archive for my reading habits, and I checked the main query for that archive URL, and the resulting MySQL request is this:

    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
    FROM wp_posts
    WHERE 1=1
    AND wp_posts.post_type = 'books'
    AND (wp_posts.post_status = 'publish'
    OR wp_posts.post_status = 'private')
    ORDER BY wp_posts.post_modified DESC
    LIMIT 0, 30
    

    I also know elsewhere on the same site, I’ve used the pre_get_posts hook to set that “30” at the very end, instead of the default posts per page of I believe “3”.

    The reason I bring this up is because the resulting query for your term archives here could be potentially including more clauses that are matching up with the Kings Cross entry enough for it to be included, or you may have your own pre_get_posts callbacks that are accidentally including more than intended.

    So my “Too Long Didn’t Read” for where to start is check the final queries being used, to see if anything is off about them.

    Thread Starter heatherdnewton

    (@heatherdnewton)

    I figured out how to fix this. Here is the solution for anyone else having a similar problem.

    I needed to wrap the content on my template in a if statement. The if statement had to be inside the while loop. The pagination needs to be outside the while loop, but it also needs the if statement applied, so I repeated the same if statement again for the pagination section.

    I also added something that allows me to label the pagination links with the term the user is browsing.

    The top of my loop now looks like this:

    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_slug=get_query_var('term');
        $term=get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
        $tax_term_name=$term->name;
    	$args = array(
    		'post_type' => 'prototype-games', // custom post type name
            'posts_per_page'    => 10, //important for a PHP memory limit warning
    		'orderby' => 'date',
    		'order' => 'DSC',
            'paged'=> $paged,
            
    	);
     
    $loop = new WP_Query( $args );
        
    	if( $loop->have_posts() ):
    				
    		while( $loop->have_posts() ): $loop->the_post(); global $post;
                if (has_term($tax_term_slug, $tax)) {

    And the bottom looks like this:

    }
    		    endwhile;
                //Pagination
                if (has_term($tax_term_slug, $tax)) {
                  echo '<div class="row med-margin-top text-center">';
                  echo '<h3><strong>' . $tax_term_name . ' Prototypes</h3>';
                  echo previous_posts_link( __( '&laquo; Previous', 'textdomain' ) ) .' | ';
                  echo next_posts_link( __( 'Next &raquo;', 'textdomain' ), $loop->max_num_pages);
                  echo '</strong></div>';
                }
        endif;
        // Clean up after the query and pagination.
        wp_reset_postdata(); 
    	
    }
    
    // Runs the Genesis loop.
    genesis();
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Good to hear you got this figured out @heatherdnewton. Let us know if you need anything else.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show only posts with a specific taxonomy term in taxonomy archive template’ is closed to new replies.