Home Forums Front-end Issues Orderby Post Object Title, not Post_ID?


Orderby Post Object Title, not Post_ID?

  • Dear All,

    I am trying to set up a WP_Query where the order is sorted alphabetically by the post title of a Post Object.

    My specific example is a list of books, where each book as an author post object that links to an author page.

    I have successfully displayed the list of books (post type – publication) with author names (post type – people), but when I try to sort by the meta_value it sorts by the Post_ID number, whereas I would like it to sort by the post title.

    It feels like I’m overlooking something simple, so I hope someone else has encountered the same problem and can help me find a solution.

    Thanks for your time.


    My code looks like this:


    $options3 = array(
    ‘post_type’ => ‘publication’,
    ‘meta_query’ => array(
    ‘key’ => ‘author’,
    ‘posts_per_page’ => ‘-1’,
    ‘orderby’ => ‘meta_value title’,
    ‘order’ => ‘ASC’

    $query3 = new WP_Query( $options3 );
    // run the loop based on the query
    if ( $query3->have_posts() ) {


    <?php while ( $query3->have_posts() ) : $query3->the_post(); ?>
    <a href=”<?php echo get_permalink( $publication->ID ); ?>”>

    <?php echo get_the_post_thumbnail($publication->ID, ‘medium’ ); ?>

    <h4><?php echo get_the_title( $publication->ID ); ?></h4>

    <?php if( get_field(‘author’, $publication->ID) ): ?>
    <p class=”pub-sub-head”>Author:

    <?php $post_objects = get_field(‘jsis_author’);

    if( $post_objects ): ?>

    <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
    <?php setup_postdata($post); ?>

    <?php the_field(‘first_name’, $post->ID); ?> <?php the_field(‘last_name’, $post->ID); ?>

    <?php endforeach; ?>

    <?php endif; ?>
    <?php endif; ?>

    </a> </li>
    <?php endwhile;
    wp_reset_postdata(); ?>

  • The relationship field => author is stored in the database as a post ID. (if it is a post object field, it’s an array of post IDs if it’s a relationship.)

    On top of that, if I understand you correctly, you are trying to order your publication posts by the title of your author posts. So you’re trying to order the posts by the title of other posts. This is not going to be possible using just a WP query.

    You’re going to need to get all the authors in the order you want them. Then you’re going to need to get all the publications and then you’re going to have to figure out how to order the publications by the authors. While possible, that’s complicated just to think about.

    You could create an acf/save_post action and in that action you could get the selected author, get the authors name and store the name in another custom field using update_post_meta(). I would hide this field by adding an _ to the beginning like _author_name The you could sort your publications by this hidden custom field. This would be a lot less work then trying to sort post by the titles of related posts.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.