Order WP Messages with a Custom Meta Key

I created a special type of WordPress post to be able to create events, select the date of the event and display the date on the interface.

I added a new meta_key to the postmeta of the WP database to keep the event date in the UNIX timestamp.

I had no problem creating a new WP request to display my events on my site, but I'm trying to figure out how to organize events by their UNIX timestamp in the database, and not by the date the WordPress events were created.

I can't seem to wrap my head around a thing .. any tips?

+4
source share
2 answers

I believe your request may have

'orderby' => 'meta_value_num', 'meta_key' => 'event_timestamp' //or whatever your meta_key is 

you can read about it here: http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

+12
source

Better to use pre_get_posts :

 function ta_modify_main_query($query) { if ($query->is_main_query()) { $query->set('orderby', 'meta_value_num'); $query->set('meta_key', '_liked'); $query->set('order', 'DESC'); } } add_action( 'pre_get_posts', 'ta_modify_main_query' ); 
0
source

All Articles