WordPress.org

Ready to get started?Download WordPress

Forums

Index of all post titles sorted by taxonomy; memory or pagination issue? (2 posts)

  1. Eos Rose
    Member
    Posted 1 year ago #

    I have created a page template that lists all terms within a custom taxonomy and thereunder all posts that are tagged with that term:

    <?php /*
    Template Name: Post Index by Duration
    */ ?>
    
    <?php get_header(); ?>
    
    <?php // List posts by taxonomy term
    $taxonomy = 'duration';
    $terms = get_terms($taxonomy,array('hide_empty' => 0));
    foreach ($terms as $term) {
       echo "TERM NAME: $term->name COUNT: $term->count<br />";
    }
    foreach ($terms as $term) {
       $args = array(
          'post_type' => 'post',
          'ignore_sticky_posts' => 1,
          'posts_per_page' => -1,
          "$taxonomy" => $term->name,
          'orderby' => 'title',
          'order' => 'ASC',
       );
       $q = new WP_Query($args);
       if ($q->have_posts()) { ?>
          <h1><?php echo $term->name; ?></h1>
          <ol>
          <?php while ($q->have_posts()) {
             $q->the_post(); ?>
             <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
          <?php } ?>
          </ol>
    
       <?php }
    
    }
    
    ?>
    <?php get_footer(); ?>

    The code works, except that it seems that search may take up too much memory. My first foreach statement calls up a list of all terms and the number of posts that should be listed:
    TERM NAME: 0:10:00-0:30:00 COUNT: 60
    TERM NAME: 0:30:00-1:00:00 COUNT: 18
    TERM NAME: 1:00:00-1:30:00 COUNT: 8
    TERM NAME: 1:30:00-2:00:00 COUNT: 3
    TERM NAME: 2:00:00-2:30:00 COUNT: 2
    TERM NAME: 2:30:00-3:00:00 COUNT: 0
    TERM NAME: 3:00:00-3:30:00 COUNT: 1
    TERM NAME: 3:30:00-4:00:00 COUNT: 0
    TERM NAME: 4:00:00-4:30:00 COUNT: 0
    TERM NAME: 4:30:00-5:00:00 COUNT: 1
    TERM NAME: 5:00:00-5:30:00 COUNT: 0
    TERM NAME: 5:30:00-6:00:00 COUNT: 1
    TERM NAME: 6:00:00 and over COUNT: 1
    TERM NAME: under 0:10:00 COUNT: 74

    Unfortunately, "TERM NAME: under 0:10:00 COUNT: 74" does not display in the second foreach, presumably because there are too many posts for the query to handle.

    My best guess is that I need to work in some pagination somehow, but I'm not sure how to modify my query to do that. Any advice would be greatly appreciated. I have five taxonomies and a LOT of posts that I'd like to index.

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I suspect that the problem is not due to memory, or to a pagination issue, but rather to the fact that terms can be applied to post_types other than 'post' and your second loop only selects 'post'.

    To test this, try this as your second loop:

    foreach ($terms as $term) {
       $sql = "SELECT tr.*,tt.*,p.ID,p.post_type,p.post_title
          FROM $wpdb->term_relationships tr, $wpdb->term_taxonomy tt, $wpdb->posts p
          WHERE tt.term_id = $term->term_id
          AND tr.term_taxonomy_id = tt.term_taxonomy_id
          AND p.ID = tr.object_id
       ";
       $rows = $wpdb->get_results($sql);
       foreach ($rows as $row) {
          // print_r('<p>ROW:');print_r($row);print_r('</p>');
          echo "TERM NAME: $term->name COUNT: $term->count POST_TYPE:$row->post_type <br />";
       }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic