Ask your WordPress questions! Pay money and get answers fast! (more info)

Sort Custom Post Type by category name WordPress

  • SOLVED

Hello all,

I have a custom post type named "coaches"

That post type is using regular categories. The categories are named like the following:

17-2
16-1
18-1
15-3

Since the category names are actually numbers I'm struggling to list the posts in order. Currently I can only get the coaches to order by title. I'd like my wp_query to order the coaches by category name/number, like the following:

18-1
17-2
16-1
15-3

Link to the live page:
[[LINK href="http://wisconsinjuniors.com/staff/coaches/"]]Live Page[[/LINK]]

Link to Pastebin:
[[LINK href="http://pastebin.com/cwf9YejZ"]]Pastebin[[/LINK]]

Answers (1)

2013-09-26

Arnav Joy answers:

try this


<div class="coaches-list clearfix">

<?php

echo '<div class="clearfix">';

$categories = get_categories('orderby=name');

if( !empty( $categories ) ){
foreach( $categories as $category ){

$args = array(
'cat' => $category ->term_id,
'post_type' => 'coaches',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);

$i = 1;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<div class="single-coach">
<a href="<?php the_permalink(); ?>" title="Read <?php the_title(); ?>'s bio">
<?php
$coachname = get_the_title();
$coachphoto = str_replace(' ', '-', $coachname );
$coachphotofull = $coachphoto .".jpg";
$dir = wp_upload_dir();
$fullPhoto = $dir["path"]."/".$coachphotofull;

if( file_exists($fullPhoto) ) {

$urlPhoto = "http://wisconsinjuniors.com/morethanaclub/wp-content/uploads/".$coachphotofull;?>
<img src="<?php echo $urlPhoto ?>" alt="<?php the_title(); ?>" />

<?php
} else { ?>

<img src="http://wisconsinjuniors.com/morethanaclub/wp-content/uploads/wisconsin-juniors-profile-placeholder.jpg" alt="Wisconsin Juniors placeholder profile image" />

<?php
} ?>
</a>

<a href="<?php the_permalink(); ?>" title="Read <?php the_title(); ?>'s bio" class="coach-name"><?php the_title(); ?></a>

<?php
$categories = get_the_category();
$separator = ', ';
$output = '';
if($categories) {
foreach($categories as $category) {
$output .= '<a href="/teams-and-players/'.$category->cat_name.'">'.$category->cat_name.'</a>'.$separator;
}
echo trim($output, $separator);
} ?>
<br />
<?php
if( get_field( 'email_address' ) ): ?>
<a href="mailto:<?php the_field( 'email_address' ); ?>" title="Email <?php the_title(); ?>" target="_blank">Email</a>
<?php
endif; ?>

</div>

<?php
if($i % 4 == 0) {echo '</div><div class="clearfix">';}

$i++; endwhile; endif;
echo '</div>';
wp_reset_postdata();
}
}
?>

</div>


Dave Schmidt comments:

Pretty close, but there's two (2) things I need help with.

I just need the order to be reversed. Currently they're listed out by lowest number to highest number (10-1, 11-1, 18-1) but I need them to go from highest to lowest (18-1, 11-1, 10-1).

The second thing I could use some help with, is getting the i+ logic to work again. I'm adding in a <div class="clearfix"> after every four (4) coaches listings (to make sure > IE9 doesn't break).


Arnav Joy comments:

try this

<div class="coaches-list clearfix">



<?php



echo '<div class="clearfix">';



$categories = get_categories('orderby=name&order=DESC');


$i = 1;

if( !empty( $categories ) ){

foreach( $categories as $category ){



$args = array(

'cat' => $category ->term_id,

'post_type' => 'coaches',

'orderby' => 'title',

'order' => 'ASC',

'posts_per_page' => -1

);





$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>



<div class="single-coach">

<a href="<?php the_permalink(); ?>" title="Read <?php the_title(); ?>'s bio">

<?php

$coachname = get_the_title();

$coachphoto = str_replace(' ', '-', $coachname );

$coachphotofull = $coachphoto .".jpg";

$dir = wp_upload_dir();

$fullPhoto = $dir["path"]."/".$coachphotofull;



if( file_exists($fullPhoto) ) {



$urlPhoto = "http://wisconsinjuniors.com/morethanaclub/wp-content/uploads/".$coachphotofull;?>

<img src="<?php echo $urlPhoto ?>" alt="<?php the_title(); ?>" />



<?php

} else { ?>



<img src="http://wisconsinjuniors.com/morethanaclub/wp-content/uploads/wisconsin-juniors-profile-placeholder.jpg" alt="Wisconsin Juniors placeholder profile image" />



<?php

} ?>

</a>



<a href="<?php the_permalink(); ?>" title="Read <?php the_title(); ?>'s bio" class="coach-name"><?php the_title(); ?></a>



<?php

$categories = get_the_category();

$separator = ', ';

$output = '';

if($categories) {

foreach($categories as $category) {

$output .= '<a href="/teams-and-players/'.$category->cat_name.'">'.$category->cat_name.'</a>'.$separator;

}

echo trim($output, $separator);

} ?>

<br />

<?php

if( get_field( 'email_address' ) ): ?>

<a href="mailto:<?php the_field( 'email_address' ); ?>" title="Email <?php the_title(); ?>" target="_blank">Email</a>

<?php

endif; ?>



</div>



<?php

if($i % 4 == 0) {echo '</div><div class="clearfix">';}



$i++; endwhile; endif;

echo '</div>';

wp_reset_postdata();

}

}

?>



</div>


Dave Schmidt comments:

That worked, thanks! I just did some CSS magic to fix the clearfix issue.