I have this query to show custom post type "property", and it works really well but the "orderby" statement, that is completely ignored...

$args = array(
    'post_type' => CUSTOM_POST_TYPE1,
    'post_status' => 'publish',
    'meta_key' => 'estado',
    'meta_value' => 'Activa',
    'orderby'=> 'modified',
    'order' => 'DESC',
    'paged' => $paged,
    'posts_per_page' => $posts_per_page,
    'tax_query' => array(
            'taxonomy' => 'pcategory',
            'field'    => 'slug',
            'terms'    => $current_term->slug
$my_query = new WP_Query($args);

This is the SQL resulting from WP_Query() "$my_query->request"

  FROM wp_posts  
  INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
  INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 
WHERE 1=1  
AND ( wp_term_relationships.term_taxonomy_id IN (28) ) 
AND wp_posts.post_type = 'propiedades' 
AND (wp_posts.post_status = 'publish') 
AND ( (wp_postmeta.meta_key = 'estado' AND CAST(wp_postmeta.meta_value AS CHAR) = 'Activa') ) 
GROUP BY wp_posts.ID 
ORDER BY (select wp_postmeta.meta_value from wp_postmeta where wp_postmeta.post_id=wp_posts.ID and wp_postmeta.meta_key = 'featured_c')
ASC,post_date DESC LIMIT 0, 6

Is it possible to sort by "post_modified" custom post field?

1 Answer 1


There is an ORDER BY clause in that query that should not have been generated by those arguments. Plus, it doesn't look like a WordPress query-- note the capitalization. WordPress tends to use all uppercase for SQL functions and operators.

I suspect that there is a filter on posts_orderby or posts_clauses that is altering that query when it should not be. When I test your query the ORDER BY clause is what you would expect from those arguments-- ORDER BY wp_posts.posts_modified-- which lends weight to the theory that something is mucking up the query.

Find the function that is altering the query, post it, and I will modify the answer. Look for add_filter('posts_orderby' or add_filter('posts_clauses' in your plugins and theme.

As written, that function overwrites the ORDER BY for most every query. It is difficult to tell where/when that filter is supposed to run, but restricting it to the main query and to categories ought to help.

function category_filter_orderby($orderby) { 
  if (is_main_query() || is_category()) {
    global $wpdb; 
    //return "post_date DESC"; 
    $orderby = "(select $wpdb->postmeta.meta_value from $wpdb->postmeta where $wpdb->postmeta.post_id=$wpdb->posts.ID and $wpdb->postmeta.meta_key = 'featured_c') ASC,post_date DESC"; 
  return $orderby; 

I don't know if that should be an OR (||) or an AND (&&), or if there should be other conditionals. I'd have to read the mind of the theme designer to tell.

  • got this on /themefolder/library/functions/listing_filters.php
    – razor7
    May 6, 2013 at 18:39
  • Code: function category_filter_orderby($orderby) { global $wpdb; //return "post_date DESC"; $orderby = "(select $wpdb->postmeta.meta_value from $wpdb->postmeta where $wpdb->postmeta.post_id=$wpdb->posts.ID and $wpdb->postmeta.meta_key = 'featured_c') ASC,post_date DESC"; return $orderby; }
    – razor7
    May 6, 2013 at 18:40
  • Please edit your question to include all relevant information and code. Formatting in the comments is minimal making comments, and especially code, difficult to read. That looks like the troublesome code though.
    – s_ha_dum
    May 6, 2013 at 18:41
  • Great, changed ...'featured_c') ASC,post_date DESC"; return $orderby; to this ...'featured_c') ASC, $orderby"; and worked!. There was a bug in the themplate file!
    – razor7
    May 6, 2013 at 18:44
  • In my case, the first order by statement is needed, but the programmers forgot to add the order by parameters passed to wp_query, so to fix it I just needed to add the value or $orderby parameter ...'featured_c') ASC, $orderby";
    – razor7
    May 6, 2013 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.