Wordpress sorting by multiple custom fields

Posted on 2014-01-30
Medium Priority
Last Modified: 2014-02-18
I have a website which has the following code in, this worked up until 3.8 but now does not, can anyone tell me why or offer another equally simply way to achieve this. Put simply I have two custom fields, the first of which could be null therefor I need to show the rest but sort primarily by the first field.

$args = array(
'post_type' => 'course',
'provider' => $term,
'posts_per_page' => 50,
'paged' => $paged,
'meta_key' => 'course_startdate',
'meta_compare' => '>=',
'orderby' => 'meta_value',
'meta_value' => $date,
'order' => 'ASC',
'meta_query' => array(
        'relation' => 'OR',
            'key' => 'course_startdate',
            'value' => $date,
            'compare' => '>='
            'key' => 'course_trainingtype',
            'value' => $date,
            'compare' => '>='

Open in new window

Many thanks
Question by:ScorchD
1 Comment
LVL 15

Accepted Solution

Jagadishwor Dulal earned 2000 total points
ID: 39833014
Write your query string separately in a variable and run it

 $querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 

 $yourvariablename= $wpdb->get_results($querystr, OBJECT);

Open in new window


Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

619 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question