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.

So i have a query like this

function filter_where( $where = '' ) {
                    // posts in the last 7 days
                    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-3 days')) . "'";
                    return $where;
                }
                add_filter( 'posts_where', 'filter_where' );
                $args = array( 'posts_per_page' => 5, 
                                'meta_key' => 'homepage_toppost', 
                                'orderby' => 'meta_value_num date', 
                                'order' => 'DESC'  
                                );
                $popularpost = new WP_Query( $args );
                remove_filter( 'posts_where', 'filter_where' );

homepage_toppost is meta key with value from 1 to 5, its just numbers. NOw i need to show posts ordered by this meta key homepage_toppost then by date, but its ignoring my meta key and ordering only by date. What am i doing wrong ?

TO UPDATE WITH SOLUTION: (which was to check if meta isnt empty)

        function yoursite_posts_where($where,$query) {
          global $wpdb;
          $new_where = " TRIM(IFNULL({$wpdb->postmeta}.meta_value,''))<>'' ";
          if (empty($where))
            $where = $new_where;
          else
            $where = "{$where} AND {$new_where} AND post_date > '" . date('Y-m-d', strtotime('-5 days')) . "'";
          return $where;
        }

so full code:

function yoursite_posts_where($where,$query) {
                  global $wpdb;
                  $new_where = " TRIM(IFNULL({$wpdb->postmeta}.meta_value,''))<>'' ";
                  if (empty($where))
                    $where = $new_where;
                  else
                    $where = "{$where} AND {$new_where} AND post_date > '" . date('Y-m-d', strtotime('-5 days')) . "'";
                  return $where;
                }
                add_filter('posts_where','yoursite_posts_where',10,2);
                $args = array( 'posts_per_page' => 5, 
                                'meta_key' => 'homepage_toppost',
                              //  'meta_value' => true,
                                'orderby' => 'meta_value_num date', 
                                'order' => 'DESC'  
                                );
                $popularpost = new WP_Query( $args );
                var_dump($popularpost->request);
               // remove_filter( 'posts_where', 'filter_where' );
                remove_filter( 'posts_where', 'yoursite_posts_where' );
                $i= 0; 
                while ( $popularpost->have_posts() ) : $popularpost->the_post(); $i++;
share|improve this question
    
If you add var_dump($popularpost->request); after your query you will see that the SQL is correct. –  s_ha_dum Aug 26 '13 at 14:49
    
When I add new post without this meta added, its showing it as first. –  Goran Jakovljevic Aug 26 '13 at 17:29
    
That is how ORDER BY works in SQL. "Empties" come first. I can solve this with a filter but do not have time to do so right now. If I don't post by 12 hours or so from now, and no one else answers, ping me by replying with @s_ha_dum in the comment. –  s_ha_dum Aug 26 '13 at 18:09

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.