Support

Account

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

  • 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' );
      }
    }