denny
denny

Reputation: 111

Custom taxonomy terms not printing in order

I have multiple custom taxonomies in custom post type. I am printing the custom posts with their multiple taxonomies in loop. I am able to done that. But the taxonomies are printing in random order like this:

This is what printing in loop:

enter image description here

As you can see some data of job type is printing in Company field, Some company is printing in Skills field. How can i print them in order.

Here is my code:

$args=array("post_per_page"=>-1,"post_type"=>"jobs");
$loop=new WP_Query($args);
if($loop->have_posts()){ 
while($loop->have_posts()):$loop->the_post();
$custom_terms=wp_get_object_terms(get_the_ID(),array('companies','job-type','skills'),array('orderby' => 'name', 'order'=>'ASC','fields'=>'all'));?>
<div class="col-md-12" style="box-shadow:0px 3px 5px 2px #f4f4f4;margin:10px;background:#fff;">
<div class="media col-md-3" style="margin-top:2%;">
    <figure class="pull-left">
        <?php the_post_thumbnail('small',array('class'=>'media-object img-rounded img-responsive'));?>
        <h5 class="list-group-item-heading" style="font-weight: 400;color: #0db294;margin-bottom:3%;border-bottom:1px solid rgb(98,59,204,0.1);padding:5px;"><?php echo the_title();?> </h5>
    </figure>
</div>
<div class="col-md-6" style="margin-top:3%;">                                   
    <div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Company</label><br/><span style="text-transform:capitalize"><?php echo $custom_terms[1]->name;?></span></div>
    <div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Job Type</label><br/><?php echo $custom_terms[0]->name;?></div>
    <div class="col-md-4 col-xs-12 text-center"><label style="color: #0db294;">Skills</label><br/><?php echo $custom_terms[2]->name;?></div>                 

</div>
<div class="col-md-3 col-xs-12 text-center"><br/>

    <a href="<?php echo the_permalink();?>" class="btn btn-primary btn-block offer-btn">View Details</a>

</div>
</div>
<?php endwhile;
}

Upvotes: 0

Views: 125

Answers (2)

denny
denny

Reputation: 111

Here is the solution as @boris suggested

$args=array("post_per_page"=>-1,"post_type"=>"jobs");
$loop=new WP_Query($args);

if($loop->have_posts()){

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

$comp=get_the_term_list(get_the_ID(),'companies','',',');
$jobt=get_the_term_list(get_the_ID(),'job-type','',',');
$skil=get_the_term_list(get_the_ID(),'skills','',',');
?>

<div class="col-md-12 col-xs-12" style="margin:10px;background:#fff;margin-left:0;">
<div class="media col-md-4 col-xs-12" style="margin-top:2%;">
    <figure class="pull-left">
        <!-- <img class="media-object img-rounded img-responsive" src="" alt="placehold.it/350x250" style="height:70px;width:auto;margin:0 auto" > -->

        <h2 class="list-group-item-heading" style="font-weight: 400;font-size:19px;color: #2A5A8E;margin-bottom:3%;padding:5px;"><?php echo the_title();?> </h2>
    </figure>
</div>
<div class="col-md-6" style="margin-top:3%;">                                   
    <div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Company</label><br/><span style="text-transform:capitalize"><?php echo $comp;?></span></div>
    <div class="col-md-4 col-xs-12 text-center b-right"><label style="color: #0db294;">Job Type</label><br/><?php echo $jobt;?></div>
    <div class="col-md-4 col-xs-12 text-center"><label style="color: #0db294;">Skills</label><br/><?php echo $skil;?></div>                 

</div>
<div class="col-md-2 col-xs-12 text-center"><br/>

    <a href="<?php echo the_permalink();?>" class="btn btn-warning btn-block offer-btn">View Details</a>

</div>
</div>
<?php endwhile;
}
?>

Upvotes: 0

Boris
Boris

Reputation: 154

Use get_the_term_list to get all terms for specific taxonomy. You can also modify this to get first one if there are multiple ones.

Like this ID, 'companies', 'Company: ', ', ' ); ?>

You are getting all and ordering by name then getting by position. That is causing issues. Get each one like this manually.

Upvotes: 1

Related Questions