Support

Account

Home Forums General Issues Ordering In An Array In A Specific Category

Solved

Ordering In An Array In A Specific Category

  • Hi, I have a page on my site that lists past events by year and then within each year lists the event by month/day. I can get the code to display by year but then the month is not in chronological order OR I can get the code to display by year and chronological order but then it displays events from two categories.

    I’m looking to get events in one category (Indoor Track and Field) to display by year, and then within each year by date. Can anyone point me in the right directions? I’ve posted the code below. This code is showing both categories by year / month.

    Thanks in advance!

    $date2018 = date( '2018-12-31' );
    
        $posts2018 = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'events',
            'meta_key' => 'event_category', 
            'meta_value'=> 'Indoor Track and Field',
            //'orderby' => 'meta_value_num',
            //'order' => 'ASC',
    
                'meta_query'=> array(
                    'meta_key' => 'event_date',
                    'orderby' => 'meta_value_num',
                    'order' => 'ASC',
                    'relation' => 'AND',
    
                    array(
                        'key' => 'event_date',
                        'compare' => '<=',
                        'value' => $date2018,
                        'type' => 'DATE',
                    ),
                    array(
                        'key' => 'event_date',
                        'compare' => '>=',
                        'value' => date( "2018-01-01" ),
                        'type' => 'DATE',
                    )
                )
        ));
  • Hi neur,

    Based on what you’ve posted, I think you’re going for something like the code below. I’ve changed a number of things:

    1. The PHP date() function isn’t used in that way. It converts an integer value of seconds into a string formatted date. If you already know the date, you can pass it as a string on its own like '2018-01-01'
    2. From this, I’ve moved the year to its own variable for easy updating. The date range can then be built dynamically
    3. You have the keys needed for sorting and the keys needed for filtering jumbled with each other. I’ve updated them to be separate and added comments above each to show what they do
    4. Finally, since the dates are in the format YYYY-MM-DD, we can use the BETWEEN value for compare to pass both dates at once

    With all of that in mind, I think this should work:

    $year = '2018';
    // Build date range from year
    $date_range = array( $year . '-01-01', $year . '-12-31' );
    
    $posts2018_args = array(
        'post_type' => 'events',
        'posts_per_page' => -1,
        // Sort by custom meta_key
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        // Filter by custom meta query
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'event_category',
                'value' => 'Indoor Track and Field',
                'compare' => '=',
            ),
            array(
                'key' => 'event_date',
                'value' => $date_range,
                'compare' => 'BETWEEN',
                'type' => 'DATE',
            ),
        ),
    );
  • Hi neur,

    Based on what you’ve posted, I think you’re going for something like the code below. I’ve changed a number of things:

    1. The PHP date() function isn’t used in that way. It converts an integer value of seconds into a string formatted date. If you already know the date, you can pass it as a string on its own like '2018-01-01'
    2. From this, I’ve moved the year to its own variable for easy updating. The date range can then be built dynamically
    3. You have the keys needed for sorting and the keys needed for filtering jumbled with each other. I’ve updated them to be separate and added comments above each to show what they do
    4. Finally, since the dates are in the format YYYY-MM-DD, we can use the BETWEEN value for compare to pass both dates at once

    With all of that in mind, I think this should work:

    $year = '2018';
    // Build date range from year
    $date_range = array( $year . '-01-01', $year . '-12-31' );
    
    $posts2018_args = array(
        'post_type' => 'events',
        'posts_per_page' => -1,
        // Sort by custom meta_key
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        // Filter by custom meta query
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'event_category',
                'value' => 'Indoor Track and Field',
                'compare' => '=',
            ),
            array(
                'key' => 'event_date',
                'value' => $date_range,
                'compare' => 'BETWEEN',
                'type' => 'DATE',
            ),
        ),
    );
  • Hi neur,

    Based on what you’ve posted, I think you’re going for something like the code below. I’ve changed a number of things:

    1. The PHP date() function isn’t used in that way. It converts an integer value of seconds into a string formatted date. If you already know the date, you can pass it as a string on its own like ‘2018-01-01’
    2. From this, I’ve moved the year to its own variable for easy updating. The date range can then be built dynamically
    3. You have the keys needed for sorting and the keys needed for filtering jumbled with each other. I’ve updated them to be separate and added comments above each to show what they do
    4. Finally, since the dates are in the format YYYY-MM-DD, we can use the BETWEEN value for compare to pass both dates at once

    With all of that in mind, I think this should work:

    $year = '2018';
    // Build date range from year
    $date_range = array( $year . '-01-01', $year . '-12-31' );
    
    $posts2018_args = array(
        'post_type' => 'events',
        'posts_per_page' => -1,
        // Sort by custom meta_key
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        // Filter by custom meta query
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'event_category',
                'value' => 'Indoor Track and Field',
                'compare' => '=',
            ),
            array(
                'key' => 'event_date',
                'value' => $date_range,
                'compare' => 'BETWEEN',
                'type' => 'DATE',
            ),
        ),
    );
    
    $posts2018 = get_posts( $posts2018_args );
  • I received a reply through email and wanted to post the code in case there are others with the same issue. Here’s the code that I’m now using:

    
    $date2018 = '2018';
    $date_range_2018 = array( $date2018 . '-01-01', $date2018 . '-12-31' );
    
    $posts2018_args = array(
                'post_type' => 'events',
                'posts_per_page' => -1,
                'meta_key' => 'event_date',
                'orderby' => 'meta_value_num',
                'order' => 'ASC',
                    'meta_query' => array(
                        'relation' => 'AND',
                        
                        array(
                            'key' => 'event_category',
                            'value' => 'Indoor Track and Field',
                            'compare' => '=',
                        ),
    
                        array(
                            'key' => 'event_date',
                            'value' => $date_range_2018,
                            'compare' => 'BETWEEN',
                            'type' => 'DATE',
                        ),
    
                    ),
                );
    
            $posts2018 = get_posts( $posts2018_args );
    

    Thanks

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

The topic ‘Ordering In An Array In A Specific Category’ is closed to new replies.