Support » Fixing WordPress » Add filter to Orderby Parameter using Array

  • Using WP-Query, I’m setting the Orderby parameter using an array:

    $events_args = array(
    	'post_status' => 'publish',
    	'post_type' => 'events',
    	'meta_key' => 'start_time',
    	'meta_type' => 'DATETIME',
    	'orderby' => array( 'meta_value_num' => 'DESC', 'title' => 'ASC' ),
    	'order' => 'ASC',
    	'posts_per_page'	=> -1,
    	'meta_query' => array(
    	        array(
    		        'key'		=> 'event_date',
    		        'compare'	=> '=',
    		        'value'		=> 20150225,
    		    )
    	    )
    );

    I need to filter the first item in the array (meta_value_num). Using add_filter , how can I access the first item of the Orderby array to make the adjustments I need?

    Thank you!

Viewing 1 replies (of 1 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    You can’t. You control the query parameters, so there’s no filter for them. It’s presumed you can change them yourself directly before making the query. The only things you can filter from the WP_Query are basically the final results after it’s been turned into segments of the SQL statement.

    If you need to filter in many ways without modifying the query directly, modify the query to have its own custom filter which you can hook into elsewhere:

    'orderby' => array( 'meta_value_num' => apply_filters('custom_filter_name', 'DESC'), 'title' => 'ASC' ),

Viewing 1 replies (of 1 total)
  • The topic ‘Add filter to Orderby Parameter using Array’ is closed to new replies.