0

I want to display a list of posts ordered by a specific meta_key. My posts have the following custom fields: author_lastname and author_firstname. So far, it works good. The posts are displayed correctly. My query is:

        <?
        $category = $wp_query->get_queried_object();
        $query = new WP_Query(
            array (
                'post_type' => 'post',
                'cat' => $category->cat_ID,
                'posts_per_page' => -1,
                'orderby'   => 'author_lastname',
                'meta_key' => 'author_lastname',
                'meta_key' => 'author_firstname',
                'order' => 'ASC'
            )
        );

        if ( $query->have_posts() ) :
            while ( $query->have_posts() ) : $query->the_post(); ?>
                <li><a href="<?php the_permalink(); ?>"><?php echo get_field('author_lastname') . ', ' . get_field('author_firstname') . ' (' . count($query) . ')'; ?></a></li>
            <?
            endwhile;
            wp_reset_query();
        endif;
        ?>

Now, let's say two posts have the same author (same values in author_firstname and author_lastname).

I will get this result:

Einstein, Albert (1)
Einstein, Albert (1)
Tesla, Nikola (1)

This is not what I want. How could I get this?

Einstein, Albert (2)
Tesla, Nikola (1)

Thank you in advance for your help!

6
  • So what happens when you click on Einstein? Does it link to a page that shows all posts with that author?
    – gdaniel
    Commented Feb 14, 2014 at 4:30
  • I can post the answer tomorrow cause I'm writing from my tablet. But I think you could approach this a different vway. You could query the database for all metadatas first. Remove the duplicates, and then run a loop to count how many posts each metadata has.
    – gdaniel
    Commented Feb 14, 2014 at 4:40
  • Yes, when I click on Einstein it should link to a page that shows all posts with that author. But I don't know how to do it either (at the moment).
    – Macxim
    Commented Feb 14, 2014 at 14:57
  • instead of using custom fields to enter the author, why not create a custom taxonomy named Authors and assign the authors, as you would with categories, to each post. That would be much easier to manage in the future, and also if you use custom taxonomies wordpress already creates the link structure for you.
    – gdaniel
    Commented Feb 14, 2014 at 15:25
  • It's a very good point. My posts are actually a list of books reviews. So, there are more data than just author_firstname and author_lastname custom fields. I have also publisher, belong to X series. Would that work too. Of course I would like to order post by publisher and serie too.
    – Macxim
    Commented Feb 14, 2014 at 17:27

1 Answer 1

1

Here's a simple way to achieve what you need with Taxonomies.

Supposed you have

  1. registered a taxonomy named Authors.
  2. Created a posted and assigned one author to that post -> Albert Einstein 3.You want only the total count of posts.

Now you want to retrieve all posts that have taxonomy = Authors and print each author name and the amount of posts with that author.

//Get all authors that have been assigned to a post
$authors = get_terms("authors");

//You can use print_r to see the values in the array
echo "<pre>";
//print_r($authors);
echo "</pre>";

//Loop through each author
foreach($authors as $author){

        //Search posts with the author name
    $query = new WP_Query(array(

        "post_type"=>"post", 
        'tax_query' => 
            array(
                array(
                    'taxonomy' => 'authors',
                    'field' => 'term_id',
                    'terms' => $author->term_id
                )
            )

        ));

    echo "<h2>Found " . $query->found_posts . " posts with author = " . $author->name . "</h2>";

}

You can format the output however you want. Wordpress automatically creates the link structure (depending how your permalinks are setup). So you just need to create templates for the links wordpress created.

UPDATE:

I am assuming that when you go to /author/einstein-albert that you can already see all posts with the term einstein-albert.

If so, the you just need to form the link using the object you have in the foreach loop:

Something similar to this should be in the foreach loop:

echo "<a href='/" . $author->taxonomy . "/" . $author->slug . "'>$author->name ($query->found_posts)</a>";

That should echo <a href="/author/einstein-albert">Albert Einstein(2)</a>
9
  • Thank you, it really helps. One question though. How will I be able to order by author's last name since my taxonomy is: Albert Einstein and I can't name it Einstein, Albert because of the comma.
    – Macxim
    Commented Feb 14, 2014 at 23:41
  • That's a good question. Perhaps you could use Einstein | Albert or use another unique character. Then you could do a search and replace when before echoing those names. You could also add the names normally, than after querying the names you could add them to an array and sort the array.
    – gdaniel
    Commented Feb 15, 2014 at 0:24
  • Or I could set the name to Albert Einstein but the slug to einstein-albert and order by slug. Not very practical but still... Thanks again.
    – Macxim
    Commented Feb 15, 2014 at 0:41
  • But I don't really understand how to get the link. I tested mydomain.com/author/einstein-albert and it works fine. But what should I put exactly in <a href="...">?
    – Macxim
    Commented Feb 15, 2014 at 1:54
  • What's your permalink structure?
    – gdaniel
    Commented Feb 15, 2014 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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