• Resolved adatfalo

    (@adatfalo)


    Hi!

    I would like to change the default wordpress search orderby parameter (date to event_start_date), but isn’t work:

    function my_search_query( $query ) {
    	if ( !is_admin() && $query->is_main_query() ) {
    		if ( is_search() ) {
    			$query->set( 'orderby', 'event_start_date' );
    		}
    	}
    }
    add_action( 'pre_get_posts', 'my_search_query' );

    What am I doing wrong?

    Thx

Viewing 5 replies - 1 through 5 (of 5 total)
  • EM post meta are hidden ones, so they start with _.
    Try _event_start_date

    Thread Starter adatfalo

    (@adatfalo)

    I modifid the code to this:

    function my_search_query( $query ) {
    	// not an admin page and is the main query
    	if ( !is_admin() && $query->is_main_query() ) {
    		if ( is_search() ) {
    			$query->set('orderby', '_events_start_date');
        $query->set('order', 'DESC');
        return $query;
    		}
    	}
    }
    add_action( 'pre_get_posts', 'my_search_query' );

    But not working well, here is the page: https://tinyurl.com/y6qvny2c
    The 3rd and 4th results should be reversed (3rd is 2019.07.20, 4th is 2019.09.05)!

    Check your spelling. You are using plural which is wrong

    Thread Starter adatfalo

    (@adatfalo)

    Thanks, I deleted the ‘s’ , but the result is the same. :/ I think the _event_start_date not the right parameter.

    Plugin Support angelo_nwl

    (@angelo_nwl)

    you can try _start_ts

    e.g.

    
    function my_em_wp_query(){
    	$args = array(
    		'post_type' => 'event',
    		'posts_per_page' => 100,
    		'meta_query' => array( 'key' => '_start_ts', 'value' => current_time('timestamp'), 'compare' => '>=', 'type'=>'numeric' ),
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC',
    		'meta_key' => '_start_ts',
    		'meta_value' => current_time('timestamp'),
    		'meta_value_num' => current_time('timestamp'),
    		'meta_compare' => '>='
    	);
    
    	// The Query
    	$query = new WP_Query( $args );
    
    	// The Loop
    	while($query->have_posts()):
    	$query->next_post();
    	$id = $query->post->ID;
    	echo '<li>';
    	echo get_the_title($id);
    	echo ' - '. get_post_meta($id, '_event_start_date', true);
    	echo '</li>';
    	endwhile;
    
    	// Reset Post Data
    	wp_reset_postdata();
    }
    add_shortcode('em_wp_query','my_em_wp_query');
    
    • This reply was modified 6 years, 9 months ago by angelo_nwl.
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Search order by event_start_date’ is closed to new replies.