1

I have a page that lists some custom posts and the they are not showing in the correct Alphabetical order. As a test I have also tried listing them in order of there Ids and that does not appear in the correct order either. When it's listed as Ids they appear as if they grouped into 3 groups high numbers in the 4000s, below 200 and then 3000s, it's very odd. It works fine on my local server. Also ordering works fine in the Wordpress back end. The problem appears to happened when I moved the site to a new hosting package on 1&1. Has any had server specific ordering issues before?

I've made 2 test pages that show a simple list of just titles and IDs (in the wrong order) ID order: http://www.realpatisserie.co.uk/allergen-list/allergen-list-test-id/ title order: http://www.realpatisserie.co.uk/allergen-list/allergen-list-test/

    $args = array(
        'posts_per_page'   => -1, // show all items
        'orderby'          => 'ID', // was 'title',
        'order'            => 'ASC',
        'post_type'        => 'product_al',
        'post_status'      => 'publish'
    );
    $myposts = get_posts( $args );

      foreach ( $myposts as $post ) : setup_postdata( $post );

            $product_title = ucfirst(get_the_title());
            $product_ID = get_the_ID();

              ?>

              <tr id="p_id_<?= $product_ID ?>'">
                  <td class="title_cell"><?= $product_ID ?></td>
                  <td class="title_cell"><?= $product_title ?></td>
              </tr>

              <?php

    endforeach; 

    wp_reset_postdata();

Edit: I tried something a little different and used WP_Query and it does the same thing

    $args = array(
        'post_type' => 'product_al',
        'posts_per_page'   => -1,
        'orderby' => 'ID',
        'order'   => 'ASC',
    );
    $the_query = new WP_Query( $args );

          while ( $the_query->have_posts() ) : $the_query->the_post();

                $product_title = ucfirst(get_the_title()); 
                $product_ID = get_the_ID();

                ?>
                  <tr id="p_id_<?= $product_ID ?>'">
                      <td class="title_cell"><?= $product_ID ?></td>
                      <td class="title_cell"><?= $product_title ?></td>
                  </tr>
                  <?php

        endwhile;

        wp_reset_postdata();
2
  • Your title example looks correct to me, can you identify a particular example if where the order is incorrect. Commented Sep 11, 2017 at 15:32
  • @JacobPeattie It's all over the place: French wheat cob > Eclair chocolate > Danish Raspberry/Apricot that's backwards!
    – lomokev
    Commented Sep 11, 2017 at 18:10

2 Answers 2

2

It would be atypical for server configuration to have a direct impact on posts query, short of SQL explicitly misbehaving. Which would be quite an edge case.

The much more common case would be some code attaching to query filters for its purposes and not being precise enough to target just the queries it needs to. Issue being specific to front end alone seems to point in this direction as well.

In either case the troubleshooting would involve digging down to actual SQL queries being generated and executed. At some stage there would either be discrepancy between correct/wrong environment or you would be able to exclude SQL and move on to troubleshooting PHP side of it.

3
  • I have update my script to use WP_Query. On the testing server and live server they both generate the following SQL: SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'product_al' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') ORDER BY wp_posts.menu_order, wp_posts.ID ASC I then ran that SQL in phpmyadmin on the production server and the Ids are wrong order. At least I know it's not a Wordpress problem. Where do I go from here?
    – lomokev
    Commented Sep 11, 2017 at 20:51
  • I just removed "wp_posts.menu_order," from the end the SQL statement "ORDER BY wp_posts.menu_order, wp_posts.ID ASC" and now it returns in the proper order.
    – lomokev
    Commented Sep 11, 2017 at 20:58
  • I don't know why would menu_order be in a simple post query, it does look like something is filtering stuff into it.
    – Rarst
    Commented Sep 12, 2017 at 6:06
1

Finally got to the bottom of this, My post type (product_al) does not support menu_order but for some reason some of the older posts had gained a values for menu_order.

Any newly created post would have it's menu_order set to 0 that's why it all looked a little messed up.

To Fix it I just ran this SQL in phpmyadmin to reset menu_order on my post type (product_al):

UPDATE wp_posts  SET `menu_order` = '0' WHERE wp_posts.post_type = 'product_al' ;

Still don't know why I gained values on menu_order, I had recently moved the site but not sure how it happened.

What I did discover which is a little odd is when using WP_Query even if you specify what you want your query to be ordered on it still always adds ORDER BY wp_posts.menu_order, if WordPress had not does this it would not of been a problem.

Your Answer

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

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