WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to Sorting / Order by multiple custom fields. (2 posts)

  1. alessandroweb
    Member
    Posted 1 year ago #

    How to display posts with 'Event' custom type ordered by 'Start_Hour' and then by 'Start_Minute'?

    'Start_Hour' and 'Start_Minute' are numeric custom fields.

    I tried this, but it doesn't work:

    $args = array(
    'post_type'=>'Events',
    'orderby' => 'meta_value',
    'meta_key' => 'Start_Hour Start_Minute',
    'order' => 'ASC'
    );
    $loop = new WP_Query( $args );
  2. alessandroweb
    Member
    Posted 1 year ago #

    Resolved by myself. Hope this helps.

    function orderbyreplace($orderby) {
        return str_replace('menu_order',
                           'mt1.meta_value, mt2.meta_value',
                           $orderby);
    }

    and...

    $args = array(
      'post_type'=>'Events',
      'orderby' => 'menu_order',
      'order' => 'ASC',
      'meta_query' => array(
            array(
                'key' => 'Start_Hour',
                'value' => '',
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'Start_Minute',
                'value' => '',
                'compare' => 'LIKE'
            )
        )
    );
    
    add_filter('posts_orderby','orderbyreplace');
    $loop = new WP_Query( $args );
    remove_filter('posts_orderby','orderbyreplace');

Topic Closed

This topic has been closed to new replies.

About this Topic