WordPress.org

Ready to get started?Download WordPress

Forums

Creating an index of all posts sorted alphabetically by custom taxonomy (33 posts)

  1. Eos Rose
    Member
    Posted 1 year ago #

    I am currently trying to put together an index of all posts sorted alphabetically by my custom taxonomy 'rating'. My code has been applied to a custom page template with some success. Here is an abbreviated version of my code (since the content of all the columns isn't particularly important here):

    <h1>Fantasy</h1>
      <table>
       <?php /////////////// Fantasy ///////////////
          $args = array( 'numberposts' => 100, 'order'=> 'ASC', 'orderby' => 'title', 'genre' => 'fantasy' );
          $myposts = get_posts( $args );
          foreach($myposts as $post) :
          setup_postdata($post);
          ?>
       <tr>
          <td><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></td>
          <td><?php if(get_post_meta($post->ID, 'Author', true)) {
             echo get_post_meta($post->ID, 'Author', true);
             } ?></td>
       </tr>
       <?php endforeach; wp_reset_postdata(); // END FANTASY // ?>
      </table>

    I have two issues with this:

    1. I have 20+ total genres. This means I have to copy-paste the code a lot, which makes for a clunky, messy template. I can't think of a better way to do this, though, since some posts are assigned more than one category. If I called up all posts at once and added a 'genre' column, each title would only appear once with multiple genres listed beside it rather than multiple times, with a single genre listed each time. Is there a way to solve this that I haven't thought of, or do I need to continue using my inelegant workaround?
    2. Right now I have sorted my table alphabetically by 'title', which works alright for the most part. My issue is that I would rather sort my list alphabetically by my custom taxonomy 'rating' (which doesn't appear in the code above as I haven't added it to my table yet, sorry!) and thereunder alphabetical by title. I can't seem to find any examples of people doing something like this. Is it possible?

    Thank you for your help in advance!

  2. vtxyzzy
    Member
    Posted 1 year ago #

    You can do what you want using filters to modify the query. There is sample code in this pastebin:
    http://pastebin.com/X5s2ha5e

  3. Eos Rose
    Member
    Posted 1 year ago #

    Thank you! Before I start experimenting with this, will this address both the repetition of code and the orderby issue or just one of the issues? And will this work in a page template?

  4. vtxyzzy
    Member
    Posted 1 year ago #

    This addresses only issue #1. It will select Posts assigned at least one of the terms of the selected taxonomy and list the post under each term that it has been assigned. It does not address issue #2, but that could be solved by using additional terms in the filters.

    The code is only a sample to show how to do the query and structure the loop. You will need to adjust the output to suit your theme.

    The sample only shows the minimum output - A header for each term, and a single line for each post showing the term name and the Post title.

  5. Eos Rose
    Member
    Posted 1 year ago #

    I created a template with the following code:

    <?php /*
    Template Name: Post Index by Genre
    */ ?>
    
    <?php get_header(); ?>
    
        <?php
    
           // Use filters to sort a query on terms of a selected taxonomy.
           // See http://wordpress.mcdspot.com/2010/05/30/filters-to-modify-a-query/
           // for the code of the filters used.
    
           $taxonomy = 'genre';
           $mam_global_join = "JOIN $wpdb->term_relationships tr ON ($wpdb->posts.ID = tr.object_id)
          JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = '$taxonomy')
          JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
    
           $mam_global_fields = ", t.name AS term_name";
    
           $mam_global_orderby = "term_name ASC, $wpdb->posts.post_title ASC";
    
           $args = array(
              'ignore_sticky_posts' => 1,
              'post_type' => 'post',
           );
           $q = new WP_Query($args);
    
           $mam_global_join = $mam_global_fields = $mam_global_orderby = ''; // Turn off filters
           //print_r('<p>REQUEST:');print_r($q->request);print_r('</p>');
           //print_r('<p>POSTS:');print_r($q->posts);print_r('</p>');
    
           if ($q->have_posts()) {
              $current_term = '';
              while ($q->have_posts()) {
                 $q->the_post();
                 if ($current_term != $post->term_name) {
                    $current_term = $post->term_name;
                    echo "<h2>TERM: $current_term</h2>";
                 }
                 echo "<p>TERM:$post->term_name &nbsp; &nbsp;  $post->post_title</p>";
              }
           }
    
        ?>
    
    <?php get_footer(); ?>

    All I got was a list of the 10 most recent posts. It didn't even display the genre at all. Could you tell me what I'm doing wrong?

  6. vtxyzzy
    Member
    Posted 1 year ago #

    Did you add the filter functions referenced in the pastebin to your functions.php?

  7. Eos Rose
    Member
    Posted 1 year ago #

    That did it, thank you very much! This will save me so much time.

  8. Eos Rose
    Member
    Posted 1 year ago #

    Anyone know how to call up ALL posts using the above code? This isn't working:

    $args = array(
    	  'numberposts' => -1,
              'ignore_sticky_posts' => 1,
              'post_type' => 'post',
           );
           $q = new WP_Query($args);

    Nor is:

    $args = array(
    	  'posts_per_page' => -1,
              'ignore_sticky_posts' => 1,
              'post_type' => 'post',
           );
           $q = new WP_Query($args);
  9. vtxyzzy
    Member
    Posted 1 year ago #

    Strange, the second example works for me.

  10. Eos Rose
    Member
    Posted 1 year ago #

    When I use the second example, all I get is a blank page. When I use 'posts_per_page' => 50, it works. When I go as high as 'posts_per_page' => 100, I get a blank page again.

  11. vtxyzzy
    Member
    Posted 1 year ago #

    I can't go that high because I don't have that many posts, but -1 retrieves all posts that meet have the taxonomy.

    Uncomment the line that dumps the request and post the output here. This line:

    //print_r('<p>REQUEST:');print_r($q->request);print_r('</p>');
  12. Eos Rose
    Member
    Posted 1 year ago #

    I uncommented and tried again using -1. I still only get a blank page.

  13. vtxyzzy
    Member
    Posted 1 year ago #

    There must be a syntax error in the code. Otherwise, you would get the output of the print_r line.

  14. Eos Rose
    Member
    Posted 1 year ago #

    When I change the -1 back to 50, the pring line gives me:
    REQUEST:SELECT SQL_CALC_FOUND_ROWS wp_posts.*, t.name AS term_name FROM wp_posts JOIN wp_term_relationships tr ON (wp_posts.ID = tr.object_id) JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'genre') JOIN wp_terms t ON (tt.term_id = t.term_id) WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY term_name ASC, wp_posts.post_title ASC LIMIT 0, 50

    I don't know if that's helpful.

  15. vtxyzzy
    Member
    Posted 1 year ago #

    All the -1 should do is elimiate the 'LIMIT 0,50' from the request. It should not cause a blank page.

    Please copy and paste the code with the -1 here so the exact code can be seen.

  16. Eos Rose
    Member
    Posted 1 year ago #

    <?php /*
    Template Name: Post Index by Genre
    */ ?>
    
    <?php get_header(); ?>
    
        <?php
    
           // Use filters to sort a query on terms of a selected taxonomy.
           // See http://wordpress.mcdspot.com/2010/05/30/filters-to-modify-a-query/
           // for the code of the filters used.
    
           $taxonomy = 'genre';
           $mam_global_join = "JOIN $wpdb->term_relationships tr ON ($wpdb->posts.ID = tr.object_id)
          JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = '$taxonomy')
          JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
    
           $mam_global_fields = ", t.name AS term_name";
    
           $mam_global_orderby = "term_name ASC, $wpdb->posts.post_title ASC";
    
    $args = array(
    		  'posts_per_page' => -1,
              'ignore_sticky_posts' => 1,
              'post_type' => 'post',
           );
           $q = new WP_Query($args);
    
           $mam_global_join = $mam_global_fields = $mam_global_orderby = ''; // Turn off filters
           print_r('<p>REQUEST:');print_r($q->request);print_r('</p>');
           //print_r('<p>POSTS:');print_r($q->posts);print_r('</p>');
    
           if ($q->have_posts()) {
              $current_term = '';
              while ($q->have_posts()) {
                 $q->the_post();
                 if ($current_term != $post->term_name) {
                    $current_term = $post->term_name;
                    echo "<h2>TERM: $current_term</h2>";
                 }
                 echo "<p>TERM:$post->term_name &nbsp; &nbsp;  $post->post_title</p>";
              }
           }
    
        ?>
    
    <?php get_footer(); ?>
  17. vtxyzzy
    Member
    Posted 1 year ago #

    Sorry, I don't see anything that explains the error.

  18. Eos Rose
    Member
    Posted 1 year ago #

    Thanks for trying. This is just so baffling. I wonder if it could be something to do with my theme? I suppose it can't hurt to test it out in another theme just to see.

  19. vtxyzzy
    Member
    Posted 1 year ago #

    How many posts do you have that meet the criteria? If too many, are you possibly running out of memory?

  20. Eos Rose
    Member
    Posted 1 year ago #

    I have 155 published posts. That shouldn't be too many, should it?

  21. vtxyzzy
    Member
    Posted 1 year ago #

    I don't think so, but a lot depends on how much memory is configured and what else is using it.

    You said that 50 would work, but 100 would not. That sounds like a memory problem.

  22. Eos Rose
    Member
    Posted 1 year ago #

    Strange how I was able to make all the posts appear using all that repeating code, but this code resists me. You would think this would method would have less of a memory strain. Memory is something I know absolutely nothing about, so if you think that's it, I'm at a loss.

  23. vtxyzzy
    Member
    Posted 1 year ago #

    The repeating code only retrieved a subset of the posts at a time, so less memory was required.

    I can't guarantee that memory is the problem, but I can't think of anything else other than memory and max execution time that would quit working as the number of posts increases.

    Check the error logs for your site to see if any errors have been reported. If you do not know how to check the logs, contact your hosting service.

    Here is an article that includes some ways to increase the execution time:

    http://wordpress.mcdspot.com/2012/03/20/ways-to-increase-max-file-upload-size-and-execution-time/

  24. vtxyzzy
    Member
    Posted 1 year ago #

    I have created a different, simpler method for doing this query. You can see the results here:

    http://pastebin.com/zhaHafQv

  25. Eos Rose
    Member
    Posted 1 year ago #

    That worked like a charm! You are so amazing, thank you!

  26. Eos Rose
    Member
    Posted 1 year ago #

    Odd. I just noticed that it skips a handful of genres in the middle of the list.

  27. vtxyzzy
    Member
    Posted 1 year ago #

    I believe that it skips genres that have no posts. Try adding a test post in one of the skipped genres.

  28. Eos Rose
    Member
    Posted 1 year ago #

    One of the genres it skipped was the largest one I had (with 44 posts).

  29. vtxyzzy
    Member
    Posted 1 year ago #

    Go to Admin->Posts->Genre and make sure the counts for each genre are correct. Sometimes these counts get messed up.

    If all the counts look correct, do you have any custom post types? The query in the pastebin only looks at a post_type of 'post'. Take out that line to see if the records may be in a different post type.

  30. Eos Rose
    Member
    Posted 1 year ago #

    The counts look correct (though what would you do if they weren't correct?). The skipped genres appeared using the other codes you gave me (well, up to a certain count, anyway). I don't have any custom post types.

Topic Closed

This topic has been closed to new replies.

About this Topic