[wp-hackers] ordering by a custom field

The WordPress Web Warlock wordpress at web-warlocks.net
Wed May 12 19:34:29 UTC 2010


En/na Brian Fidler ha escrit:
> Thanks, I'm using a custom query because I'm also using the NextGen Gallery
> and I need to query based on both NGG and custom fields.
>
> Is there a way to access data from NGG with $wp_query, I wasn't able to find
> anything.
>
>   
Hello,

I haven't worked with NGG, so I don't have "much" idea about how it 
works (actually, not the least :) ). I suppose that it will use some 
custom fields of its own — and if you want to use more than a cf, yes, 
it's outside the scope of WP_Query/$wp_query, which AFAIK only adds a 
single instance of wp_postmeta. Then, you'd have to JOIN twice or more 
the wp_postmeta, and sort accordingly. Be sure to allow for NULL if you 
need to — sorting by custom fields is one of the best ways to get some 
posts "lost in limbo".

But if you can use a "true secondary loop", that is, a loop inside The 
Loop, then some get_posts with orderby=meta_value, 
meta_key=artwork_order passed with the order args.
Anyway, don't miss the good point noted by Glenn Ansley: if you intend 
to use the order as number (or date or anything not a string), be sure 
to cast/convert it or whatever in the query (and is quite possible that 
you'll need $wpdb for that), or use other tricks of the trade (like 
"zeroising" the value, which is not always feasible, &c).


More information about the wp-hackers mailing list