[wp-hackers] Global meta_key ordering for a custom post type

Werner lists at mollentze.co.za
Fri Aug 27 18:14:21 UTC 2010


  Hi Hackers,

I'm writing a plugin that enables a custom post type for WP3. The post type is 
for tasks, so I use a meta_key "priority" that I would like to sort the posts by.

Doing the widget is a snap, I'm using:

         $r = new WP_Query(
             array(
                 'post_type'    => 'custom_task_type',
                 'post_status'  => 'publish',
                 'meta_key'     => 'priority',
                 'orderby'      => 'meta_value',
                 'order'        => 'DESC',
                 'meta_compare' => '>=',
                 'meta_value'   => 5,
                 ...
             )
         );
         ...
         wp_reset_postdata();

... to sort the posts for the widget using the "priority" meta_key's value and 
only accounting for posts with a "priority" meta_value greater than 5.

*But*... for general post ordering in archive (I also have custom taxonomies) 
and single page mode (that would also influence things like get_next_post()), 
I'd like to be able to influence the global loop ordering, using the priority 
meta_key, for *any* query that involves my custom post type.

Basically, my custom post type should never be ordered by post date, under any 
circumstances - always by using the meta_key's value, otherwise the ordering 
would simply not make any sense.

What is the best hook to use in my plugin, to influence the ordering of any 
query that *involves* my custom post type? Should I go the query_posts() route?

A simple example would mean the world to me at this point.

Thanks for your time and effort if you have read this far :)


More information about the wp-hackers mailing list