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?
Lev SeltzerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

eemitCommented:
// 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

Lev SeltzerAuthor Commented:
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

eemitCommented:
I have tested that with default post type 'post'.
First run the query, then try:
var_dump($query);
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

eemitCommented:
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

Lev SeltzerAuthor Commented:
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

eemitCommented:
- 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?
eemitCommented:
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

Lev SeltzerAuthor Commented:
The posts are sorted by menu order, as shown by your debug code. Now what?
eemitCommented:
You have more posibilities:

- Find in your theme where your posts are sorted by menu_order and comment that out.
I have already mentioned pre_get_posts.
Please note: I am also hireable. In my Profile you can see Hire Me button.

- Adjust Posts Menu_order to you desire.

- Optimally I would create a custom post type for members.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lev SeltzerAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.