Hi guys, I'm trying to figure out why in the following code, the number of terms listed stops at the 3rd term.
I know their are some issues existing with pagination, but first, I'm trying to see why the term count is limited.
If I take out the following line, the code works great and displays ALL the terms in 100gridgroups.
if( get_field('star_rating', '100gridgroups_'.$lc->term_id) != 'star5' ) continue;
However, if in place, the query stops at the 3rd term. I can't figure out why but I think it's related to the pagination settings. Can I get some advice please?
My full 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(); ?>
Dbranes answers:
Hi, I guess you keep these terms <em>custom fields</em> in the <em>wp_options</em> table.
Your query:
$libcats = get_terms( '100gridgroups', $_libargs);
doesn't know about the these custom fields, so if this part:
if( get_field('star_rating', '100gridgroups_'.$lc->term_id) != 'star5' ) continue;
stops your loop after 3 turns, it just means that the <em>star_rating</em> custom field isn't <em>star5</em> anymore.
But I'm afraid I can't tell you why that is ;-)
You should try to echo the <em>get_field</em> values to check the real values.
streetfire comments:
Hm, well I have another (simpler) template - without pagination using the following code, and it works just great! It retrieves all the terms with star5 value. I just can't figure out why the one above seems to be affected by the pagination part.
<strong>Working Template with Get 5 Star Terms
</strong>
<?php
/*
Template Name: 100 List - 5 Star Groups
*/
?>
<?php get_header(); ?>
<div class="row heading-grid">
<div class="container">
<div id="heading-position">
<div class="col-md-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-md-9">
<!-- Begin LG Screen View-->
<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">
<!-- Field Query Begin -->
<?php
// Checks for page number
$paged = $wp_query->get( 'paged' );
if ( ! $paged || $paged < 2 )
{
$terms = get_terms('100gridgroups', array(
'exclude' => array(16, 20, 22,25, 27, 28, 30, 4, 33,), //* Enter ID's of parent categories to exclude from list
'hide_empty' => 0 ) );
if($terms) {
$i = 0;
foreach($terms 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> <!-- col lg 3 end-->
<?php
}
}
}
else
{
echo '<div class="col-lg-12"><a class="btn btn-info" href="http://www.runningalpha.com/focus15/">Return to Top 15</a></div>';
}
?>
</div>
<!-- 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-2 col-xs-12">
<?php include("sidebar100list.php"); ?>
</div> <!-- End col-md-3 -->
</div><!-- End row -->
</div><!-- End Container -->
<?php get_footer(); ?>
Dbranes comments:
I think what you're experiencing is that since the <em>get_terms()</em> query doesn't know about your <em>star_rating</em> values,
then with the above pagination, you could get an empty output on some pages or only few items.
Dbranes comments:
As I understand it your loop could be, for example, with the <em>star_rating</em> custom fields:
star5
star2
star1
star3
--- page 2
star4
star5
star3
star5
--- page 3
star1
star4
star2
star1
star3
--- page 4
... etc
and then you're filtering out <em>star5</em> values.
So that means some pages could give you empty output, or only few <em>star5</em> items.
The above example, filtered with <em>star5</em>, would give:
star5
--- page 2
star5
star5
--- page 3
--- page 4
... etc
So I think this is the problem.
streetfire comments:
Hi, You are exactly right. I check to see if that is what's happening. Sure enough...
Page 1
- 3 terms (with 5 stars)
Page 2
- Empty
Page 3
- 1 term (with 5 stars)
Page 4
- 1 term (with 5 stars)
Now I wonder how to fix this - if its possible with pagination?
Dbranes comments:
You have chosen a difficult path, filtering the terms with some values from the <em>wp_options</em> table isn't easy.
Regarding a solution, three possibilities come to mind:
1) Find a way to modify the SQL of the <em>get_terms</em> call to fetch only terms with <em>stars5</em>
2) Fetch all the terms without any pagination, then filter out the <em>stars5</em> terms via PHP into an array, then apply your paging scheme to that array.
3) Change the strategy.
I'm afraid #1 could be difficult if the data is stored as a serialized array.
Dbranes comments:
How does the data look like in the <em>wp_options</em> table, that you're fetching with the <em>get_field</em> function?
streetfire comments:
Hehe, yes my client requests difficult features 90% of the way through development. :) Thank you for your input. I will probably have to go with either 2 or 3 there. ;)