Support

Account

Home Forums Backend Issues (wp-admin) Sort CPT entries by an ACF field

Solved

Sort CPT entries by an ACF field

  • Hi there,
    I created a custom CPT that has a set of ACF fields. I wanted to sort the CPT entries in a custom manual way, so I added a Number ACF field to my CPT, assigned numbers for each entry and then wanted to show this number on the admin dashboard view for this CPT + sort ascending by this field. I added an extra column, populated it with the field content, made the column sortable – and this worked well. However I cannot make the entries to be sorted by default by this field. I don’t know what I’m doing wrong. I browsed through every post on StackExchange and this forum, but all solutions failed.
    Here is my code

    // Add my new column 'Order number' in the admin dashboard for the CPT 'Book' (ACF field name is 'order-no'
    add_filter( 'manage_book_posts_columns', 'set_custom_edit_book_columns' );
    function set_custom_edit_book_columns( $columns ) {
      $columns['order-no'] = __( 'Order number', 'my-text-domain' );
      return $columns;
    }
    
    // Populated the new column with data from the ACF field 'order-no'
    add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
    function custom_book_column( $column, $post_id ) {
      switch ( $column ) {
        case 'order-no' :
          echo get_field( 'order-no', $post_id );  
          break;
      }
    }
    
    // Make the new column sortable
    add_filter( 'manage_edit-book_sortable_columns', 'set_custom_book_sortable_columns' );
    function set_custom_book_sortable_columns( $columns ) {
      $columns['order-no'] = 'order-no';
      return $columns;
    }
    
    // Sort my Book entries by the 'order-no' column / ACF field [this doesn't work!]
    add_action( 'pre_get_posts', 'book_custom_orderby' );
    function book_custom_orderby( $query ) {
      if ( ! is_admin() )
        return;
      $orderby = $query->get( 'orderby');
      if ( 'order-no' == $orderby ) {
        $query->set( 'meta_key', 'order-no' );
        $query->set( 'orderby', 'meta_value_num' );
    	$query->set( 'order', 'ASC' );
      }
    }

    Thanks for any hints!

  • What part is failing?

    Is the column added?
    Is the column data added?
    Is there an input that lets you sort by the column?
    Or is it just that the posts are not being sorted?

  • @hube2 – I believe I answered that in my post.
    Anyway, the last thing doesn’t work – sorting by the ACF field. My fields are still sorted by default by date.

  • Sorry, I probably missed it.

    Check the values you are passing to the query

    
    $orderby = $query->get( 'orderby');
    echo $orderby; die;
    

    this will give tell you what the value is and exit so that you can see it.

  • This gives me, as expected, the date as the query input. However, I thought that the rest of the snippet provides different query parameters, doesn’t it?

  • Is this when you click on your custom column?

    You will need to load the page, then add the code, then click on the column.

    I’ve looked over your code and the documentation and other examples and I don’t see any reason that it should not be working.

  • The case is that manual sorting works – the column is sortable and when I click the column the order changes and is sored the way I want. However what I want to achieve is automatic sorting, i.e. when I open the post page all posts are sorted by this field/column right away, without clicking the column name.

  • your need to see if orderby is in the url query. This cannot be done by looking at $query->get( 'orderby'); because by the time of pre_get_posts WP has already set it to the default of date in the admin.

    
    if (empty($_GET['orderby'])) {
      // set default orderby
    }
    
  • Yeap, you’re right. This does the trick. Thanks!

    Just for future reference, here is my working code:

    add_action( 'pre_get_posts', 'book_custom_orderby' );
    function book_custom_orderby( $query ) {
      if ( ! is_admin() )
        return;
      if (empty($_GET['orderby'])) {
        $query->set( 'meta_key', 'order-no' );
        $query->set( 'orderby', 'meta_value_num' );
    	$query->set( 'order', 'ASC' );
      }
    }
  • Just noticed that the code change fixes my sorting issue on the CPT page, but makes the pages and posts lists empty 🙁

  • You need to check that you are only applying it to the post type in question.

    
    if (empty($query->query_vars['post_type']) || $query->query_vars['post_type'] != 'your-post-type') {
      return;
    }
    
  • You’re my hero 🙂 Thank you.

    Final code:

    add_action( 'pre_get_posts', 'book_custom_orderby' );
    function book_custom_orderby( $query ) {
      if ( ! is_admin() )
        return;
      if (empty($query->query_vars['post_type']) || $query->query_vars['post_type'] != 'book') {
      return;
      }
      if (empty($_GET['orderby'])) {
        $query->set( 'meta_key', 'order-no' );
        $query->set( 'orderby', 'meta_value_num' );
    	$query->set( 'order', 'ASC' );
      }
    }
Viewing 12 posts - 1 through 12 (of 12 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.