Custom select query wordpress order by meta key value

I am trying to ORDER BY my results of a custom select query.

But I am trying to order by the value of a meta key.

Please see my query below...

$get_atts =  $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE $wpdb->posts.post_type IN ('individual') ORDER BY $wpdb->posts.meta_key = 'surname' ASC");

As you can see this ORDER BY is breaking it...

ORDER BY $wpdb->posts.meta_key = 'surname' ASC"

So I am trying to order by the value of surname

But my does not seem to work. Can any explain why or help?

Answers


Try to use this query:

$get_atts =  $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = 'surname' 
    AND $wpdb->posts.post_type IN ('individual') ORDER BY $wpdb->postmeta.meta_value ASC");

Need Your Help

How to make a text event call on a function and how to display images in AS3?

image actionscript-3

Originally the AS3 script would trace some words for a certain string I typed in the textfield (ti). I want to know how to display and image, instead.

Gravity Forms Price based on financial quarter of year

php function gravity-forms-plugin

I'm trying to build a membership form with Gravity Forms, but I've just been informed that the membership price goes down each quarter of the year, starting from April, by £22.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.