Word Count: 216
  • Post category:Reborn
  • Post last modified:2020-12-24

This article describes the approach for sorting custom posts with sorting sequence controlled by a custom field.

  • Using ACF, add a custom field named ‘sorting_order’ for the Treatment post type.  Valid input range is from 1 to 999.  Default value is 999.
  • For existing posts, these custom fields have to be updated manually one by one – took almost 45 min for 82 posts!  Newly created posts will be given the default value automatically.
  • Bear in mind that if the field value is null / empty, the post is excluded.
  • In both the Template file and functions.php (for Load More button), add the following criteria to the WP_Query argument array. This will sort the posts by ‘sorting_order’ in ascending order. If posts have the same sorting order value, the post created most recently will come in front in the sequence.
'orderby' => array('meta_value_num' => 'ASC', 'date' => 'DESC'),
'meta_key' => 'sorting_order',

  • In both the Template file and functions.php (for Load More button), create the ‘data-sort’ attribute inside the h4 tag for post title as below.
<h4 data-sort="<?=get_field('sorting_order'); ?>"><?= the_title(); ?></h4>

  • To look up the sorting order value of the concerned post at the front-end, right-click the post title to call up the Dev Tool.  Look for the ‘data-sort’ attribute.