0

TLDR: My custom post type items are sorting differently on a local environment and a live server. Can't figure out why!

I have a local install (MAMP) and an online dev install (GoDaddy shared, for the client to view) of a site with pages of Custom Post Type posts. The CPT posts are real estate properties. The CPT posts have meta info added using Advanced Custom Fields. The ACF fields include the state each property is in and the type of property.

Here is a relevant page CPT page for Wisconsin posts

The CPT posts are divided into three pages by state. Within each state page, the properties are sorted by type of property. That is it - they are sorted into their states and then sorted by type of property.

I used a WP_query to do this for each page. Here is the query for the Wisconsin page:

<?php 
  $args = array(
    'post_type' => 'properties',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'meta_query' => array(
      'state_clause' => array(
        'key' => 'state', 
        'value' => 'WI'
       ),
       'prop_type' => array( 
         'key' => 'property_type'
       ),
     ),
     'orderby' => array(
       'prop_type' => 'ASC'
     )
   );
  $wiQuery = new WP_Query($args);
?>
<?php
  if( $wiQuery->have_posts() ) {
    while ($wiQuery->have_posts()) : $wiQuery->the_post();

The sort is working fine for the state pages. In other words, all the Wisconsin properties appear on the right page, and they are grouped property in order of the ACF meta field for Property Type.

However, within those groups the properties appear in a different order on the local MAMP site than they do on the dev install. For example, on the local install, 1501 Paramount Drive appears before 1209 Deming Way. Which seems really odd to me!

The dev site and the local site are both the same version of WordPress and the exact same custom theme I’m working on. They are using the same version of ACF. 95% of the posts were added to the local site before it was uploaded to the dev location.

Could it be differences with PHP versions (seems unlikely) or something about the mysql version?

The Properties custom post type itself has a weird origin. A developer set it up many years ago using the Easy Custom Post Type plugin, which no longer works. I deactivated that plugin and added the CPT to the functions file and I was able to use all the properties that were added with ECPT. I just had to redo all the meta data.

This is not an urgent issue, it’s just baffling. If anyone has any ideas of why the sort would be different on different servers I’d be grateful to hear them.

3
  • 1
    PHP versions wouldn't be causing this as it's MySQL that does the sorting, can you confirm that they both have the same plugins activated on the same versions with the same settings? I tried helping someone recently who was seeing unexpected search behaviour and it turned out they had buddyboss installed which was replacing standard WP search with its own, and others have had pre_get_posts filters that were too broad
    – Tom J Nowell
    Jan 25, 2022 at 0:59
  • 1
    Also, if you ignore the numbers and go from the first letter they do look like they're alphabetically sorted, though I would expect it to take the number into consideration too. I'd also avoid settings posts_per_page to -1, set it to a high number you never expect to reach or that you know is the upper limit of what your server can handle, but never infinity, that way you can guarantee the page will never timeout or run out of memory. The REST API uses ~100 posts as an upper limit per page for this reason
    – Tom J Nowell
    Jan 25, 2022 at 1:02
  • They both do have the same plugins, WP version, php version. Thanks for the tip about the posts_per_page issue - I made the change to my code!
    – Ann Foley
    Jan 26, 2022 at 15:07

2 Answers 2

1

A php issue retuning different results on 2 or more synced environments (if each env db data is the same and any passed data is the same)... that could be tricky.

The only other thing I noticed is your meta_query arg does not contain a child array containing state_clause and prop_type.

Your code is...

'meta_query' => array(
  'state_clause' => array(
    'key' => 'state', 
    'value' => 'WI'
  ),
  'prop_type' => array( 
    'key' => 'property_type'
  )
),

You might need to wrap both inner meta_query args arrays into another array, see example below...

...and possibly you may need to include a relation argument/key... see the end of the orderby WP_Query docs

'meta_query' => array(
  array(
    'state_clause' => array(
      'key' => 'state', 
      'value' => 'WI'
    ),
      'prop_type' => array( 
      'key' => 'property_type'
    )
  )
),
1
  • Thanks for the tips about the loop arrays - it makes sense and I changed my code!
    – Ann Foley
    Jan 26, 2022 at 15:08
0

Here's a possible answer to the question - the mysql versions seem to be different on the two installs. Good enough for me, for now. Thanks everyone for helping out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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