WordPress.org

Ready to get started?Download WordPress

Forums

WP_Query meta_value sorting (4 posts)

  1. violagrr
    Member
    Posted 1 year ago #

    I have a news article post type with an attribute called "news_publish_year_hidden" which stores a year, such as 2009. I am trying to write a query to get all the articles written in 2009 and 2012. The code does not work and it returns all the news articles if I try to sort the results by another custom attribute called "news_publish_date_hidden" which stores a timestamp.
    The following is my query:

    $myArray = array(			
    
                        'relation' => 'OR',
    		    array(
    			    'key' => 'news_publish_year_hidden',
    			    'compare' => '=',
                 		    'value' => '2009'
    			  ),
    
    		    array(
    			    'key' => 'news_publish_year_hidden',
    			    'compare' => '=',
    			    'value' => '2012'
    			  )								  									  									 									  );
    
    $news = new WP_Query(
                            array(
                                  'post_type'=>'news',
                                  'meta_key' => 'news_publish_date_hidden',
                                  'order' => DESC,
                                  'orderby' =>meta_value',
                                  'paged'=>get_query_var('paged'),
                                  'posts_per_page'=>'2',
                                  'meta_query' => $myArray
                                  )
    );
  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this:

    $args =  array(
      'post_type'=>'post',
    	'paged'=> get_query_var('paged'),
    	'posts_per_page'=> -1,
    	'meta_query' => array(
    	  array(
    	    'key'     => 'news_publish_year_hidden',
    	    'value'   => array('2012', '2009'),
    	    'compare' => 'IN',
    	    'type'    => 'NUMERIC'
    	  )
    	),
    	'meta_key' => 'news_publish_date_hidden',
    	'orderby'    => 'meta_value',
    	'order'       => 'DESC',
    );
    
    $news = new WP_Query($args);
  3. violagrr
    Member
    Posted 1 year ago #

    Thank you very much!this is exactly what U have tried and it worked!:)

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you got it resolved :-)

Topic Closed

This topic has been closed to new replies.

About this Topic