Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(
        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"

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID 
  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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
got this on /themefolder/library/functions/listing_filters.php –  razor7 May 6 '13 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 '13 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 '13 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 '13 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 '13 at 18:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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