1

I'm not an expert at much, so please excuse me if this is an easy question... but it's got my head spinning.

I have an archive template (adapted from a standard 'enfold' theme template) which I am using to display a simple list of books. Following a tutorial I found elsewhere, I created the loop below.

If I understand the code correctly, the first part of the block below is used to fetch and sort the taxonomy values for the custom post type. So, it finds 'Romance', 'Horror', 'Humour' etc. It then displays posts with those taxonomy values together. So, I have a little table with a taxonomy value as a heading, and the posts with that taxonomy value in the table. Then I have a table for the next taxonomy type, and the relevant posts, one for each taxonomy value.

The 'orderby' in the original code sorts the taxonomy values by name.

It works perfectly, except the posts inside each 'taxonomy-table/group' are in random order, and I need to sort them by post title.

I want to sort the posts (inside their taxonomy-sorted tables) by title. How do I do this? I've tried copying every bit of code I can find on the subject but nothing has come close to working.

The loop used is;

<?php //start by fetching the terms for the booktype taxonomy
$terms = get_terms( 'booktype', array(
    'orderby'    => 'name',
    'order'   => 'DESC',
    'hide_empty' => 0
) );
?>

<?php
foreach( $terms as $term ) {
    // Define the query
    $args = array(
        'post_type' => 'fiction',
        'booktype' => $term->slug
    );
    $query = new WP_Query( $args );

    // output the book type in a heading tag                
    echo'<h4>Book Type ' . $term->name . '</h4>';
    echo '<table>';
    echo '<tr>';
    echo '<th>Title</th>';
    echo '<th>Author</th> ';
    echo '<th>Published</th>';
    echo '<th>ISBN</th>';
    echo '<th>Sales</th>';
    echo '</tr>';
    while ( $query->have_posts() ) : $query->the_post();
        ?>
        <tr>
            <td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>
            <td><?php the_field( 'book-author' ); ?></td>
            <td><?php the_field( 'book-published' ); ?></td>
            <td><?php the_field( 'book-isbn' ); ?></td>
            <td><?php the_field( 'book-sales' ); ?></td>
        </tr>
    <?php endwhile;
    echo '</table>';
    wp_reset_postdata();
} ?>

If I try to edit the array in the second block (below) and add an orderby to that, it appears to kill the first block. I just get empty tables with headings but no posts.

    // Define the query
    $args = array(
        'post_type' => 'fiction',
        'booktype' => $term->slug
    );
    $query = new WP_Query( $args );

What else can I try?

0

You should modify this fragment:

// Define the query
$args = array(
'post_type' => 'fiction',
'booktype' => $term->slug,
'orderby' => 'name',
'order' => 'ASC'
);
  • Thank you for your suggestion, but it didn't work. I have edited my original question to reflect that.. – Adoptable Jan 3 '17 at 13:06
  • To clarify, any changes simply caused all the posts (regardless of taxonomy value) to be displayed in the table created to output posts with each taxonomy value. So the table which currently has just books with the tax "Romance" also contains "Sci-fi", "Horror" and so on. This is repeated for every taxonomy value. – Adoptable Jan 3 '17 at 17:23
0

What you want to do is update your call to WP_Query specifying the orderby parameter as title and your order, either ASC or DESC.

// Define the query
$args = array(
    'post_type' => 'fiction',
    'booktype' => $term->slug,
    'orderby' => 'title',
    'order'   => 'DESC',
);
  • Thank you for your suggestion, but it didn't work. I have edited my original question to reflect that.. – Adoptable Jan 3 '17 at 13:06
0

Quick and dirty would be to have a foreach getting all the categories and sorting them alphabetically, then a foreach inside that to get all the posts within said category and sorting them alphabetically.

Not too fond of nested foreach, but it should do the trick.

  • Thanks for that but I wouldn't know where to start - my coding skills are on a par with a retarded pigeon. I did a quick search to see how I would start it, but almost every post I came across on the subject was flagging problems or saying 'don't do it', so I'm guessing your lack of fondness for nested foreach has a good justification! – Adoptable Jan 3 '17 at 17:18
0

With wp_query it is possible to give an array in the orderby parameter. You can even give different order criteria. Like this:

$args = array(
    'post_type' => 'fiction',
    'orderby' => 'name title',
    'order'   => 'ASC',
    );

or

$args = array(
    'post_type' => 'fiction',
    'orderby' => array( 'name' => 'DESC', 'title' => 'ASC' )
    );

This should enable you to do a multidimensional sort (needs WP 4.0 or up).

  • Thanks for your suggestion. I tried it, but it doesn't work. If I add the title or custom field name into the orderby, it DOES start sorting by name, but pulls all the posts into all the taxonomy tables, giving me a couple of thousand posts instead of the current 600 or so. I tried a variety of things and the result was either as explained above, or a white screen. As there is already an orderby (sorting the taxonomy) does this conflict with the second one? Or maybe there's a mistake in the original code? – Adoptable Jan 3 '17 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.