Link to home
Start Free TrialLog in
Avatar of Lev Seltzer
Lev SeltzerFlag for Israel

asked on

How to sort Posts by META key (custom field) in WordPress?

A client had a page (http://aleh.org/aleh-staff/) which displayed certain posts sorted by a custom field. this page suddenly stopped working and the posts were being displayed in a different order.
To create the desired order, the programmer used PHP code in a custom template.

I modified the template to display the sort field (you can see that on the live page), so you can see how the order is incorrect.
<br>Sort: <?php echo get_post_meta(get_the_ID(), 'members_order_number', true) ?>

Open in new window


The code to create the query when I found it was:
      $args = array (
          'cat'                    => $catid,
          'posts_per_page'         => -1,
          'order'                  => 'DSCE',
          'orderby'                => 'date',
		  'meta_key'		=> 'members_order_number',
		  'orderby'		=> 'meta_value_num',
		  'order'			=> 'DESC'
  
      );
      // The Query
      $query = new WP_Query( $args );

Open in new window

I made a few changes and it still does not work:
      $args = array (
      	'cat'				=> $catid,
		'posts_per_page'	=> -1,
	    'orderby' 			=> 'meta_value_num', 
		'meta_key'			=> 'members_order_number',
		'order'				=> 'DESC'  
      );
      // The Query
      $query = new WP_Query( $args );

Open in new window


What am I missing to get this to sort properly?
Avatar of eemit
eemit
Flag of Germany image

// Try this:
$args = array (
	//'post_type' => 'post',
	//'post_status' => 'publish',
	'cat' => $catid,
	'posts_per_page' => -1,
	'orderby' => 'meta_value_num',
	'order' => 'DESC',//or ASC
	'meta_query' => array(
		array(
			'key' => 'members_order_number',
			'type' => 'NUMERIC'
		),
	),
);
// The Query
$query = new WP_Query( $args );

Open in new window

Avatar of Lev Seltzer

ASKER

it does not work, as you can see from the page http://aleh.org/aleh-staff/
I added some code to show me the query string $args
foreach($args as $arg){
    echo $arg;echo '<br>';
}

Open in new window


Full code is below. Any more suggestions? Thank you.

   // WP_Query arguments
  $args = array (
	'cat' => $catid,
	'posts_per_page' => -1,
	'orderby' => 'meta_value_num',
	'order' => 'DESC',//or ASC
	'meta_query' => array(
		array(
			'key' => 'members_order_number',
			'type' => 'NUMERIC'
		),
	),
);
foreach($args as $arg){
    echo $arg;echo '<br>';
}
// The Query
$query = new WP_Query( $args );
      // The Loop
      if ( $query->have_posts() ) {

Open in new window

I have tested that with default post type 'post'.
First run the query, then try:
var_dump($query);
Try this:
			$meta_value = get_post_meta( get_the_ID(), 'members_order_number', true );
			echo "<p>members_order_number: $meta_value</p>";

Open in new window

after the line:
		while( $query->have_posts() ): $query->the_post();

Open in new window

I have added the code you suggested. It is very busy, and a bit too confusing to me. Do you understand what you see at http://aleh.org/aleh-staff/ to know what is not sorting properly.
For reference, below is the current code.

   // WP_Query arguments
  $args = array (
	'cat' => $catid,
	'posts_per_page' => -1,
	'orderby' => 'meta_value_num',
	'order' => 'DESC',//or ASC
	'meta_query' => array(
		array(
			'key' => 'members_order_number',
			'type' => 'NUMERIC'
		),
	),
);
// The Query
$query = new WP_Query( $args );
var_dump($query); 
      // The Loop
      if ( $query->have_posts() ) {
          while ( $query->have_posts() ) {
              $query->the_post();
$meta_value = get_post_meta( get_the_ID(), 'members_order_number', true );
			echo "<p>members_order_number: $meta_value</p>";			  $_permalink = get_the_permalink();

Open in new window

- You can comment out test lines:
var_dump($query);
$meta_value = get_post_meta( get_the_ID(), 'members_order_number', true );
echo "<p>members_order_number: $meta_value</p>";                   

- As I have already said, this is tested and it works well here.
- It seams that your posts support “menu_order” field for posts.
- It seams that your posts are sorted by “menu_order”
Your sort order is maybe changed using:
add_action('pre_get_posts', ...
in your theme's functions.php file.

- If your posts support “menu_order” field for posts
(you should see 'page-attributes' metabox on posts edit screen),
then you can use menu_order for sorting and you don't need custom field, and 'meta_query', e.g.:

// WP_Query arguments
$args = array (
	'post_type' => 'post', 
	'post_status' => 'publish', 
	'cat' => $catid,
	'posts_per_page' => -1,
	'orderby' => 'menu_order',
	'order' => 'DESC',//or ASC
);

// The Query
$query = new WP_Query( $args );
      // The Loop
      if ( $query->have_posts() ) {
          while ( $query->have_posts() ) {
              $query->the_post();

Open in new window


- Can you post here your page template in which you have placed this code?
To test if your posts are sorted by menu_order:
add this line:
      
global $post;

Open in new window

below the line:
      
$query = new WP_Query( $args );

Open in new window


and add this line:
      
echo "<p>menu_order: $post->menu_order</p>";

Open in new window

below the line:
      
while( $query->have_posts() ): $query->the_post();

Open in new window

The posts are sorted by menu order, as shown by your debug code. Now what?
ASKER CERTIFIED SOLUTION
Avatar of eemit
eemit
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I found a plugin called "Post Types Order" and deactivated it. The posts now sort in perfect order. I don't know why the client needed that plugin, so perhaps we can leave it disabled. Otherwise, I will have to discuss various solutions with the client (and may click on your "hire me" button if I need further help.

Thank you.