Home Forums Front-end Issues Custom Post Type: Order posts by a group's field at archive page


Custom Post Type: Order posts by a group's field at archive page

  • Hello guys,

    I am trying to fetch all custom posts : missions
    and sort them by a date picker field named: mission_date_start

    I have created with ACF a fields group for my custom post type posts -> I created among others a group field named: mission_date that has two childs:
    mission_date_start: type: date picker – return format ‘Ymd’ – key: field_5b9be275c6962
    mission_date_end : type: date picker – return format ‘Ymd’

    I created my archive page and has these arguments for my WP_Query

    $today = date(‘Ymd’);
    $args = array (

    ‘post_type’ => ‘missions’,

    ‘orderby’ => ‘meta_value’,

    ‘order’ => ‘ASC’,

    ‘post_status’ => ‘publish’,

    ‘posts_per_page’ => -1,

    ‘meta_key’ => ‘mission_date_start’,

    ‘meta_query’ => array(
    ‘key’ => ‘mission_date_start’,
    ‘value’ => $today,
    ‘type’ => ‘DATE’,
    ‘compare’ => ‘>=’



    This is what is suggested from various places this question is asked. It doesn’t work.

    I tried to change the values of ‘meta_key’ and ‘key’ to the fields key instead of name (field_5b9be275c6962) and doesn’t work.

    I suspect that might be a problem that the field is inside a group field?? Don’t know…

    Can someone help me?

  • This should work with something like:

    $args = array(
            'post_type' => 'your_custom_post_type',
            'meta_key'=> 'your_field_date',
            'orderby' => 'meta_value',
            'order' => 'ASC' 

    If you’re are working with the archive page, maybe a best way to do it is modify the main query with the pre_get_posts hook.

    For Example:

    // in your functions.php
    function sort_by_date_my_cpt( $query ) {
       if ( is_post_type_archive( 'your_custom_post_type') ) {
          $query->set('meta_key', 'your_date_custom_field');
          $query->set('orderby', 'meta_value');
          $query->set('order', 'ASC');
    add_filter( 'pre_get_posts', 'sort_by_date_my_cpt', 1);

    So then, you will to use your WP Loop by default in your archive template.

    // in your archive-your_custom_posttype.php or archive.php
    <?php if ( have_posts() ) :
       <?php while ( have_posts() ) : the_post(); ?>
       <?php endwhile; ?>
    <?php endif; ?>
  • dgaitan thank you for your reply.

    I tried both ways suggested!
    The pre_get_posts hook solutions looks so clean … though either did the trick!

    When I placed the arguments suggested at the first solution no custom posts are fetched on my archive page.
    When I edit the pre_get_posts hook it even erased all the text from the rest of the page (header and footer).

    The only way it fetches results is when I remove from my args the meta_key and order_by options. Using this way though all custom posts are ordered by the date a custom post is published.

    I find it really weird.

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

The topic ‘Custom Post Type: Order posts by a group's field at archive page’ is closed to new replies.