1

I'm running a site for a book store. Each book has a catalogue number. These numbers are often a string with numbers in it. For example- PUB 1 would be a common entry, as well as PUB 12. Right now, it's sorting PUB 10 after PUB 1 but before PUB 2. What do I need to do to have its sort order go: PUB 1, PUB 2... PUB 9, PUB 10?

Here's my current code.

<?php
$labelargs= array(
    'post_type'      => 'book',
    'posts_per_page' => 1000000,
    'meta_query'     => array(
        'relation' => 'AND',
        array(
            'relation'  => 'OR',
            'otherinfo' => array(
                'key'     => 'wpcf-other-information',
                'compare' => 'EXISTS',
            ),
            'otherinfonotexists' => array(
                'key'     => 'wpcf-other-information',
                'compare' => 'NOT EXISTS',
            ),
        ),
        'wpcf_catalogue_number' => array(
            'meta_value_num' => 'ASC',                //I don't think this is the right place for this
            'key'            => 'wpcf-catalogue-number',
        ),
        'parent_release_year' => array(
            'key' => 'wpcf-parent-release-year',
        ),
        'release_year' => array(
            'key' => 'wpcf-release-year',
        ),
        'format' => array(
            'key' => 'wpcf-format',
        ),
    ),
    'orderby'        => array(
        //I tried 'meta_value_num' => 'ASC', here but it didn't work
        'wpcf_catalogue_number' => 'ASC',
        'format'                => 'ASC',
        'parent_release_year'   => 'ASC',
        'release_year'          => 'ASC',
        'otherinfo'             => 'ASC',
        
    ),
);


?>

1 Answer 1

2

The documentation for WP_Query indicates that ordering by meta value is limited to one meta key and requires the meta_key parameter (untested):

$results = new WP_Query( array(
    'meta_key' => 'wpcf_catalogue_number',
    'orderby'  => array(
        'meta_value_num' => 'ASC',
    ),
) );

If you want additional sorting, you'll need to sort the result ($results->posts) using PHP (untested):

$sort__formats = array();
// Other sorting arrays.

foreach ( $results->posts as $post ) {
    $formats[] = get_post_meta( $post->ID, 'format', true );
    // Other values added to sorting arrays.
}

array_multisort( $results->posts, $sort__formats, SORT_ASC, ... );

This won't be highly performant, so you'll likely want to consider a more performant approach, like combining all the meta values into one, and sorting off of that.

5
  • Thanks Caleb. Can you share a bit more about what function I would use to sort that? How can I find more information about that type of code?
    – DJZEEGLER
    Jun 19, 2023 at 15:43
  • 1
    Updated my answer.
    – Caleb
    Jun 19, 2023 at 15:52
  • I updated my code to use $query = new WP_Query($labelargs); foreach ( $query->posts as $post ) { $catnumbers[] = get_post_meta( $post->ID, 'wpcf-catalogue-number', true ); // Other values added to sorting arrays. } array_multisort($catnumbers, SORT_ASC, SORT_NATURAL) ; print_r($catnumbers);. the print_r is in the right order, but the results of my query are not. How do I resolve that?
    – DJZEEGLER
    Jun 19, 2023 at 16:22
  • 1
    Add $query->posts as first parameter of array_multisort(). See code in answer.
    – Caleb
    Jun 19, 2023 at 16:24
  • got it! needed to do it like this: array_multisort($catnumbers, SORT_ASC, SORT_NATURAL, $query->posts) ;. Thanks so much!
    – DJZEEGLER
    Jun 19, 2023 at 16:32

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.