0

I am using which code which returns taxonomies which match 2 values.

Everything works as it should, but I can't figure out how to order my results. Right now they are displayed in some set order, it might be date not sure. I am trying to get them to display alphabetically (by name)..

My Code from my php template is pasted here http://pastie.org/5083124

The array I am talking about is this

<?php
    foreach ( $all_terms as $all_term) {
    //print_r($all_terms);

        $tax_test = get_option('woo_categories_panel_taxonomies_'.$all_term->taxonomy);

            $post_images = array();
            $posts_aray = array();
            $parent_id = $all_term->term_taxonomy_id;
            $term_name = $all_term->name;
            $term_parent = $all_term->parent;
            $term_slug = $all_term->slug;
            $term_id = $all_term->term_id;
            $term_link = get_term_link( $all_term, $all_term->taxonomy );
            $counter_value = $all_term->count;

            ?>
            <div class="childListings">
                <div class="block">
                    <a href="<?php echo $term_link; ?>">
                        <?php
                            $block_counter++;
                         ?>
                    </a>

                    <h2><a href="<?php echo $term_link; ?>"><?php echo $term_name ?> <br/><span>(<?php echo $counter_value; ?> Solicitors)</span></a></h2>

                </div><!-- /.block -->
            </div><!-- /.child Listings-->
            <?php

            if ( $block_counter % 6 == 0 ) {
            ?>
                <div class="fix"></div>
            <?php
            } // End IF Statement   

        // End IF Statement

        ?>
        <?php


    } // End For Loop

?>

I have looked at a few different options with $args and ksort, but I get a bit lost and can't seem to get my results on the frontend of the site to be sorted alphabetically.

Can anyone identify in my code how I would be able to have my results have a sort order?

Thanks

3 Answers 3

4

You can avoid bothering to sort in the PHP by sorting slightly earlier, when you're querying the database. This should be faster.

Change:

$all_terms = $wpdb->get_results("SELECT *  FROM ipt1y7_term_taxonomy,ipt1y7_terms WHERE ipt1y7_term_taxonomy.parent='{$ex[2]}' AND ipt1y7_term_taxonomy.term_id=ipt1y7_terms.term_id");

...to:

$all_terms = $wpdb->get_results("SELECT *  FROM ipt1y7_term_taxonomy,ipt1y7_terms WHERE ipt1y7_term_taxonomy.parent='{$ex[2]}' AND ipt1y7_term_taxonomy.term_id=ipt1y7_terms.term_id ORDER BY ipt1y7_terms.name");

i.e. just add ORDER BY name to your original query. The results will be returned sorted by name with no need for you to do anything further in the PHP, and the sort will happen on the database server. The WordPress database table terms has an index on the name column, so this should be very fast; effectively the data is pre-sorted for you. (See the description of the terms table in the WP database schema.)

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know WordPress allowed writing your own query. This is definitely better than my solution.
0

Have a look at the examples in http://php.net/manual/en/function.sort.php

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

The above example will output:

fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange

2 Comments

Hi thanks, yeah I found a similar example before with the fruits, but I was unsuccessfully about to understand how to apply that to my code above. Could you point me in the right direction specifically to my code? thanks a lot
I should maybe emphasise that my terms within the array are not hard coded like you have in this example with lemon, apple etc...If you look at my pastie above I match 2 taxonomies at the top of the page if that makes sense
0

This is possible using usort and your own comparison function:

<?php

/**
 * For a bit of testing.
 */
$all_terms = array( );
$names = array( 'foo', 'baz', 'bar', 'qux', 'aaa' );
foreach( $names as $name ) {
    $tmp = new stdClass();
    $tmp->name = $name;
    $all_terms[] = $tmp;
}

/**
 * Here, we do the sorting:
 */
usort( $all_terms, function( $a, $b ) {
    if( $a->name === $b->name ) {
        return 0;
    }
    return ( $a->name < $b->name ) ? -1 : 1;
});

/**
 * And the results:
 */
var_dump( $all_terms );

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.