Want wp_get_post_terms return in arbitrarily order, how to do?

Function below returns in alphabetical order and not, as wished, in arbitrarily order.
Is it possible to -not- return in alphabetical but in exact order as given in the array?

add_filter('the_content', 'my_function');
function my_function($content) {
if ( is_single() && in_category( '5' ) ) {
the_post_thumbnail('thumbnail', array('class' => 'top-post-img' ));    

global $post;
$term_list=wp_get_post_terms($post->ID,array('school','article','teacher'),array( "fields" => "names"));
?>
<div style="margin:45px;">
    School: <strong><?php print_r($term_list[0]); ?></strong><br />
    Article: <strong><?php print_r($term_list[1]); ?></strong><br />
    Teacher: <strong><?php print_r($term_list[2]); ?></strong>
</div>
<?php       
return $content;
} else { 
return $content;
}
}

I tried by adding:

Read More
array ('orderby' => 'post__in'),  array ('post__in' => '24', '10', '3', '46' )

Numbers are the Taxonomy ID’s but it is not changing any, neither does it throw an error.

To find the Taxonomy ID’s I used following code, or is that the wrong code?

$terms = get_the_terms( $post->ID , 'taxonomy-name' );
if($terms) {
foreach( $terms as $term ) {
    echo $term->term_id.'<br />';
}
}

Should I use list_filter somewhere, or some below in the code after the print_r ?

Added

 var_dump($term_list);

to see if output would show some else, but no it was the same..alphabetical order.
I am pretty sure forgetting some but what, and where. Or is my approach totally wrong?

Would love to find a solution.
Thanks in advance for all time and effort.

Related posts

2 comments

  1. add_filter('the_content','my_function');
    function my_function($content){
        the_post_thumbnail('thumbnail', array('class' => 'top-post-img' ));
    
      if (is_single() && in_category('5')){
        global $post;
        $term_list = wp_get_post_terms ($post->ID,'school',array('fields'=>'names'));
        ?>
        <div id="some_class">
        <span>School:<strong><?php print_r($term_list[0]); ?></strong></span>
        <?php
        if (is_single() && in_category('5')){
            $term_list = wp_get_post_terms ($post->ID,'article',array('fields' =>'names'));
            ?>
            <span>Article:<strong><?php print_r($term_list[0]); ?></strong></span>
            <?php
            if (is_single() && in_category('5')){
                $term_list = wp_get_post_terms ($post->ID,'teacher',array('fields'=>'names'));
                ?>
                <span>Teacher:<strong><?php print_r($term_list[0]); ?></strong></span>
                </div>
                <?php
                return $content;
                }
            }
        } else {
            return $content;
        }
    }
    

    Output as shown in Post

    Thanks to suggestions made by Rarst, this code works like a charm.
    As you can see, it does show the Featured Image which belongs to that post,
    and the output is returned in exact order as wished in the code.
    It also uses some CSS to style (in the style.css) so it shows nice below
    the title and above the content.

    Update
    Getting Notice: Undefined offset: 0

    Anyone around to help me out?

  2. If you take a look at source for wp_get_object_terms() which a lot of related functions use internally, there is not option for custom orderby in it. I would probably just re-sort returned data in PHP for this.

    However even more practically I would just retrieve terms for different taxonomies separately. What you have there looks tad fragile logically – for example what if one of terms is missing, or there are more than expected assigned by mistake? Output will go bad fast.

Comments are closed.