0

I have pages with a custom field "language" which can be, say, "EN", DE" or "FR".

On a category page I want to output them in a specific order, say, first EN pages, then FR pages, then DE pages.

It seems to only be possible to order "language" by ASC or DESC. Is there a way to sort in a custom order provided in an array?

  • What other order could there possibly be? You can't sort in an other way than DESC or ASC... Thats just generally not possible! – veelen Nov 4 '14 at 21:15
  • You can order ASC A-B-C or DESC C-B-A, or custom, e.g. "consonants first" B-C-A. – Steve222 Nov 6 '14 at 9:48
0

Your best option is to use a taxonomy.

If EN, DE and FR are terms then you then loop through them and get the pages on a per term basis.

You can do this in conjunction with a plugin that enables you to manually re-order terms such as this one.

https://wordpress.org/plugins/i-order-terms/

You can then do something like this with your code:

// Get all of the terms in the taxonomy
$countries = get_terms( 'countries' );

// Check to ensure there are results returned
if ( !empty( $countries ) && !is_wp_error( $countries ) ) {

    // Loop through the countries
    foreach($countries as $country) {

        $args = array(
            'post_status' => 'publish',
            'post_type' => 'page',
            'countries' => $country->slug,
        )

        // Get all of the pages that have this term
        $country_pages = new WP_Query($args);

        // If there are results then print out the country's title and then a list of pages for that country.
        if($country_pages->have_posts()) : ?>

            <h2><?php echo $country->name; ?></h2>

            <ul>

                <?php 
                while($country_pages->have_posts()) : $country_pages->the_post(); ?>

                    <li><?php the_title() ?></li>

                <?php endwhile; 
                // Ensure that the main query is reset
                wp_reset_postdata(); ?>

            </ul>

        <?php 
        endif; 

    }
}
  • Ah ok, I thought that I might have to make separate WP_Querys for each language. I have hardcoded the priority in an array instead of using terms, but it's good to know that I can make the setting available in the admin area defining this as a term. – Steve222 Nov 6 '14 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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