This topic contains 0 replies, has 1 voice, and was last updated by  nicos 7 months, 2 weeks ago.

  • Author
    Posts
  • #9213

    nicos
    Participant

    Hello,
    I try to sort events custom post type by start timestamp, but it doesn’t work

    function get_events($event_types=null, $periode=null, $posts_per_page=-1) {
    
        $type_args = array();
        if($event_types) {
            $type_args = array(
                'key' => 'event_type',
                'compare' => 'IN',
                'value' => $event_types,
            );
        }
    
        $past_args = array(
            'key' => 'event_fin_timestamp',
            'compare' => '<',
            'value' => strtotime(date("Y-m-d")),
        );
    
        $present_args =  array(
            'relation' => 'AND',
            array(
                'key' => 'event_debut_timestamp',
                'compare' => '<=',
                'value' => strtotime(date("Y-m-d")),
            ),
            array(
                'key' => 'event_fin_timestamp',
                'compare' => '>=',
                'value' => strtotime(date("Y-m-d")),
            ),
        );
    
        $future_args = array(
            'key' => 'event_debut_timestamp',
            'compare' => '>',
            'value' => strtotime(date("Y-m-d")),
        );
    
        $periode_args = array();
        if($periode) {
            if($periode == 'past') $periode_args = $past_args;
            if($periode == 'present') $periode_args = $present_args;
            if($periode == 'future') $periode_args = $future_args;
        }
        
        $events_query = new WP_query(
            array(
                'post_type' => 'event',
                'posts_per_page' => $posts_per_page,
                'order_by' => 'meta_value',
                'meta_key' => 'event_debut_timestamp',
                'order' => 'ASC',
                'meta_query' => array(
                    $type_args,
                    $periode_args
                )
            )
        );
        return $events_query->posts;
    
    }
    

    Anyone could help me, or send me a link about this subject ?
    Thank you, and sorry for my poor english :/

You must be logged in to reply to this topic.