Support

Account

Home Forums Backend Issues (wp-admin) Sorting admin columns by post-meta post-object title.

Helping

Sorting admin columns by post-meta post-object title.

  • I have a custom post type (Testimonials) that, in the admin post table, I want to be able to sort by College, Area and Course. These fields are post-objects in each testimonial post.

    If we take the College column, for example, it seems to be ordering my post by some unknown variable, and not alphabetically (as can be seen below). It doesn’t appear to be ordering by postID. The Name (using the title) column sorts fine, but all of my sortable columns using post-objects are sorting funny.

    Admin columns to be sorted

    My code to sort the content is as follows:

    add_action( 'pre_get_posts', 'divinity_sortTestimonialColumns', 1 );
    
    function divinity_sortTestimonialColumns( $query ) {
    	
       // Run our code in the main WP query AND if an orderby query variable is designated
        
       if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
          switch( $orderby ) {
          	
    		case 'college':
             	
                // set our query's meta_key for our college custom field
                $query->set( 'meta_key', 'author_course_college' );
                $query->set( 'orderby', 'meta_value' );
                
                break;
          }
       }
    }

    Any ideas on how to sort by post-object titles?

  • It is ordering by the post ID of the post object selected… unless these post object fields allow multiple values, in this case it is ordering by an a serialized array value.

    There isn’t a way to order posts alphabetically by the title of a the post selected in a post object field. The title belongs to the post selected and not to the post you are trying to order. In order to do this the post title of the other post must be a meta value associated with the post you are trying to order. You need to create an acf/save_post filter, get the value of the post object field, get the title of the post object and store that value in another meta field and then use this meta field to sort the posts.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.