Hey guys, you might have seen my other question as well. It's related to this, but please answer other question without regards to this.
I need to order a list of terms by the value of a custom field associated to the taxonomy terms.
I.e. My field is Star Rating and I need the terms to be listed from 5 star rating down to 0 stars.
The challenge for me is trying to figure out how this will work with pagination.
<strong>Solution Ideas</strong>
1) Is there is a way to tell my template (which is calling terms from my taxonomy) to order those terms by a field value?
2) If #1 is not doable, I am thinking I can create a template which calls 5 star groups first, then 4, 3, etc. using something like this:
if( get_field('star_rating', '100gridgroups_'.$lc->term_id) != 'star5' ) continue;
However, again, I'm not 100% sure how I can make the pagination work correct. Currently my pagination reads ALL the terms in my taxonomy and creates a new page after 12 terms. Perhaps if I leave my pagination structure as is, I can actually remove the call to get all my terms in alphabetical order, and then insert my if statements grabbing the star ratings in the order I want. I wonder if pagination will still work right.
UPDATE:
This is a template I'm currently using to display a paginated list of ALL my terms. Currently there is no sorting by star rating - it is by alphabetical order only.
<?php
/*
Template Name: 100 List Page
*/
?>
<?php get_header(); ?>
<div class="row heading-grid">
<div class="container">
<div id="heading-position">
<div class="col-lg-12">
<h2><span class="heading-grid-title"><span class="glyphicon glyphicon-th icon-white"></span> <?php the_title(); ?></span></h2>
<div id="breadcrumbs"> <small><?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?></small></div>
</div>
</div>
</div><!-- close container -->
</div><!-- close row services -->
<div id="spacer"></div>
<div class="container">
<div class="row">
<div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
<article> <!-- Grid Key -->
<div class="row">
<div class="col-lg-12">
<?php include("grid-key.php"); ?>
</div></div>
<!-- Grid Key -->
<!-- Content Protection -->
<?php ob_start();?>
<!-- Content Protection -->
<div class="row" >
<?php
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var('paged');
}elseif( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
}else{
$paged = 1;
}
$per_page = 12;
#fix
$term_args = array(
'hide_empty' => 0,
'exclude' => array(16, 20, 22,25, 27, 28, 30, 4, 33,), //* Enter ID's of parent categories to exclude from list
);
$number_of_terms = wp_count_terms( '100gridgroups' , $term_args); // This counts the total number terms in the taxonomy with a function)
$paged_offset = ($paged - 1) * $per_page;
$libargs = array(
'orderby' => 'name',
'order' => 'ASC',
'exclude' => array(16, 20, 22,25, 27, 28, 30, 4, 33, ), //* Enter ID's of parent categories to exclude from list
'number' => $per_page,
'offset' => $paged_offset,
);
$_libargs = wp_parse_args($term_args, $libargs);
$libcats = get_terms( '100gridgroups', $_libargs);
#fix
$i = 0;
foreach($libcats as $lc){
if( $i % 4 == 0 ) { ?>
<div class="clearfix"></div>
<?php }
$i++; ?>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3" >
<?php $termlink = get_term_link( $lc->slug, '100gridgroups' ); ?>
<div class="panel panel-default">
<div class="panel-image">
<div class="thumbnail">
<div class="caption hidden-md hidden-sm hidden-xs">
<br/>
<h5><?php echo $lc->name; ?> </h5>
<!-- Popover Start -->
<div class="po-markup" ><a href="<?php echo $termlink;?>" class="p5598 popover-dismiss btn btn-info btn-lg"><?php echo $lc->count ?> Symbols</a>
<br/><h4><a class="label" href="<?php echo $termlink;?>">Open Group</a></h4>
<div class="po-content hidden popover">
<div class="po-title">
<div class="row"><div class="col-lg-9">
<h4><?php echo $lc->name; ?> Symbols </h4></div><div class="col-lg-3"><h4><span class="label label-info pull-right"><?php echo $lc->count ?> Active</span></h4></div></div>
</div> <!-- ./po-title -->
<div class="po-body">
<?php if(mgm_user_is(array('grid100'))) {
include("symbol-list-popover.php");
} else {
echo '<div class="widget-area"><div class="alert alert-info"><span class="glyphicon glyphicon-eye-close"></span> You need to be a Grid 100 subscriber to see the full content.<br/><br/> <a class="btn btn-md btn-info" href="http://www.runningalpha.com/subscriptions/grid100/"> Sign Up for Grid 100</a></div></div>';
}?>
<br/>
<a class="btn btn-navy btn-block" href="<?php echo $termlink; ?>">View All <?php echo $lc->count ?> Symbols</a>
</div><!-- ./po-body -->
</div> <!-- ./po-content -->
</div><!-- ./po-markup -->
<!-- Popover End --> <br/>
</div>
<!-- Get Image by Attachment ID Start-->
<?php
$attachment_id = get_field('taximage', '100gridgroups_'.$lc->term_id);
if ($attachment_id) {
$image = wp_get_attachment_image_src($attachment_id, 'industrygroup-img');
if ($image) {
?>
<img class="img-responsive" src="<?php echo $image[0]; ?>" />
<?php
}
}
else { ?>
<img class="img-responsive" src="http://www.runningalpha.com/wp-content/uploads/2014/09/RA-logo-300px-groups.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
<!-- description div -->
<div id="deschide">
<div class="<?php if(get_field('level_of_interest', '100gridgroups_'.$lc->term_id)) {
echo the_field('level_of_interest', '100gridgroups_'.$lc->term_id);
} else { ?>intleveldefault<?php } ?>">
<!-- description content -->
<div class="description_content"> <a href="<?php echo $termlink; ?>"><?php echo $lc->name; ?></a> </div>
<!-- end description content -->
</div> </div>
<!-- end description div -->
</div>
<!-- Get Image by Attachment ID End-->
</div>
<div class="panel-footer text-center">
<!-- Icons -->
<div class="row">
<!-- LG Screen -->
<div class="col-lg-4 visible-lg">
<div class="<?php if(get_field('heat_level', '100gridgroups_'.$lc->term_id)) {
echo the_field('heat_level', '100gridgroups_'.$lc->term_id); } else { ?>heatleveldefault<?php } ?>"></div>
</div>
<div class="col-lg-8 visible-lg">
<div class="<?php if(get_field('star_rating', '100gridgroups_'.$lc->term_id)) {
echo the_field('star_rating', '100gridgroups_'.$lc->term_id);} else { ?>stardefault<?php } ?>"></div></div>
<!-- LG Screen -->
<!-- MD, SM, XS Screen -->
<div class="hidden-lg col-md-12 col-sm-12 col-xs-12">
<center> <div class="<?php if(get_field('heat_level', '100gridgroups_'.$lc->term_id)) {
echo the_field('heat_level', '100gridgroups_'.$lc->term_id); } else { ?>heatleveldefault<?php } ?>"></div></center>
</div>
<div class="hidden-lg col-md-12 col-sm-12 col-xs-12">
<center> <div class="<?php if(get_field('star_rating', '100gridgroups_'.$lc->term_id)) {
echo the_field('star_rating', '100gridgroups_'.$lc->term_id);} else { ?>stardefault<?php } ?>"></div></center></div>
<!-- MD, SM, XS Screen -->
</div><!-- end row -->
<!-- Icons -->
</div>
</div>
</div>
<?php } ?>
</div>
<!-- Pagination Start -->
<div class="text-center">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links(
array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => ceil( $number_of_terms / $per_page ), // 20 items per page
'type' => 'list',
'end_size' => 5,
'mid_size' => $mid_size,
)
);
?></div>
<!-- Pagination End -->
<!-- Content Protection -->
<?php echo do_shortcode('[private]'.ob_get_clean().'[/private]' ); ?>
<!-- Content Protection -->
</article>
</div> <!-- End col-md-9 -->
<div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">
<?php include("sidebar100list.php"); ?>
</div> <!-- End col-md-3 -->
</div><!-- End row -->
</div><!-- End Container -->
<?php get_footer(); ?>
John Cotton answers:
My understanding is that 5-Star, 4-Star etc are taxonomy terms. If that's wrong, then the following question is wrong as well.
Does the page that needs paginating contain a list of taxonomy terms, or posts that are tagged with the terms?
If the latter, what should the sorting be for posts that have the same term?
streetfire comments:
Hi John,
Let me clarify. 5-star, 4 Star etc are not terms. They are values of a custom field assigned to a term.
I.E.
Taxonomy: Grid 100 Groups
Term: Silver
Star Rating (custom field): 5 Star
The page that needs paginating is a list of taxonomy terms.
I will update my question with some code.
John Cotton comments:
Since you're using get_terms to retrieve the list, you'll need to add some code to hook in to the 'terms_clauses' filter.
Basically, you'll need to filter the join and order clauses of the get terms query to join to your custom field values and sort by that value.
Are you using Advanced Custom Fields or something like that for your taxonomy custom fields?
If so, the revised SQL will be a bit extended and difficult to advise on without actually seeing your database.
streetfire comments:
Yes I am using Advanced Custom Fields. I'm afraid your solution might be a bit out of my own reach to implement.