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

Pagination Needs to Only Count Terms with Set Field Value WordPress

  • SOLVED

Hi guys.

I'm using the template below to display a list of terms that have a custom field value set. The custom field is Star Rating and is tied to the taxonomy term. This template checks if the Star Rating is "star5" and displays the terms if they are.

However, this template was copied from another page, thus the pagination is currently searching for basically ALL terms within my taxonomy "100gridgroups".

I need a solution so my pagination only counts the terms inside Star Rating > "star5".

Is there a way to do this?

My code:

<?php

/*

Template Name: 100 List Page by Star Order

*/

?>

<?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( get_field('star_rating', '100gridgroups_'.$lc->term_id) != 'star5' ) continue;

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(); ?>

Answers (2)

2014-10-21

Bob answers:

Not sure if this will work or not but give it a try!

It seems your paginate code depends on total parameter, so here,<strong> $number_of_terms</strong> is important variable.

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,
)
);


But that variable is set with all terms count!

$number_of_terms = wp_count_terms( '100gridgroups' , $term_args);

That's why it will take all count and show more pages then require.

Not the solution is set the right amount to terms in <strong>$number_of_terms</strong> variable.

To do so go to foreach loop which loop through all terms.
Right before it set $number_of_term to 0

and right after condition <em>if( get_field('star_rating', '100gridgroups_'.$lc->term_id) != 'star5' ) continue; </em>
increase it's value by adding <strong>$number_of_term++</strong>

So the old code would be

foreach($libcats as $lc){
if( get_field('star_rating', '100gridgroups_'.$lc->term_id) != 'star5' ) continue;



And New code would be

$number_of_terms = 0;
foreach($libcats as $lc){
if( get_field('star_rating', '100gridgroups_'.$lc->term_id) != 'star5' ) continue;

$number_of_terms++;


Note: This is just an Idea I have not tested it! Try it and let me know what happens :)


streetfire comments:

Hi Thank you for your advice. I was going to try your method, but in the process found for some reason my template is not loading any term after the 3rd term. I'm trying to figure out why. So, I will get back to you :|


Bob comments:

Okay no problem!


Bob comments:

Hi Streetfire,

Did you above solution worked for you?


streetfire comments:

Bob, Please see my comments below, thanks!

<blockquote>Hi, I'm sorry for the delay. No I do not have a solution yet. :( You see I found an issue with the terms being loaded on different pages and not all on the first couple pages with this template. Please review this question: http://wpquestions.com/question/showChronoLoggedIn/id/10007

DBranes diagnosed what was going on and since I've been quite stuck.

I would still like to show a paginated list of terms with the 5 star field value set, but it seems much more difficult than I thought. If either of you have another solution I'm open!

I'd also raise the pot if you had something that worked. :)</blockquote>


<blockquote>
P.S. Please advise on what I should do for payment. I realize you already put in effort and should be compensated. I am happy to close this thread and reward you the prize money for your effort. I am also happy to raise the pot a little bit (to $30) if it would help me fix this problem I have.

Suggestions welcome!</blockquote>

2014-10-22

Arnav Joy answers:

Hi ,

Is your problem solved ?

-Arnav


streetfire comments:

Hi, I'm sorry for the delay. No I do not have a solution yet. :( You see I found an issue with the terms being loaded on different pages and not all on the first couple pages with this template. Please review this question: http://wpquestions.com/question/showChronoLoggedIn/id/10007

DBranes diagnosed what was going on and since I've been quite stuck.

I would still like to show a paginated list of terms with the 5 star field value set, but it seems much more difficult than I thought. If either of you have another solution I'm open!

I'd also raise the pot if you had something that worked. :)


streetfire comments:

P.S. Please advise on what I should do for payment. I realize you already put in effort and should be compensated. I am happy to close this thread and reward you the prize money for your effort. I am also happy to raise the pot a little bit (to $30) if it would help me fix this problem I have.

Suggestions welcome!