[wp-trac] [WordPress Trac] #19653: Order by meta field forces ignore of null records

WordPress Trac noreply at wordpress.org
Sun Nov 4 11:36:14 UTC 2012


#19653: Order by meta field forces ignore of null records
-------------------------+------------------------------
 Reporter:  tomauger     |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Query        |     Version:
 Severity:  normal       |  Resolution:
 Keywords:               |
-------------------------+------------------------------

Comment (by GeertDD):

 +1 for this enhancement. I really would like to see the option to easily
 create a `LEFT JOIN` too.

 For now I pulled it off by hooking into the `posts_join_paged` filter,
 like this:

 {{{
 #!php
 add_filter('posts_join_paged', 'use_my_join'), 10, 2);

 function use_my_join($join, $wp_query)
 {
         // Note: check some $wp_query->query_vars here first,
         // since the custom join is not needed for all queries.

         global $wpdb;
         $join = "LEFT JOIN $wpdb->postmeta
                 ON $wpdb->posts.ID = $wpdb->postmeta.post_id
                 AND $wpdb->postmeta.meta_key = 'my_custom_field_name'";

         return $join;
 }
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/19653#comment:2>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list