skip to Main Content

Wordpress : Fix pagination display on Custom Post Type ArchivesWordPress : Fix pagination display on Custom Post Type Archives

While working on a current project. I encountered a common problem: “Archive page pagination shows initial few pages and upon clicking higher pages, the link goes to 404”.

In the process of working on the custom archive page and using “get_term()”, i discovered that this was also counting empty terms and/or terms with draft entries thus confusing the “pagination”.

Since I am an older programmer and have yet to learn OOP, MVC, etc.. I have come up with a very basic (that I agree needs to be updated soon) solution.

// this was my original get_terms query
$terms = get_terms( 'family', array('orderby' => 'name','hide_empty' => 0) );
// here is a simple or basic routine to check for empties
/////-------------------------------------->>>>
$etn_tot_valid_terms=0;
// initialize variable to hold array of terms to exclude
$etn_exlude_these='';
// using the above get_terms,
foreach ($terms as $term) {
// for each term i query the wp_postmeta to check for entries under the plant family with the value of the slug
$etn_chk_empty=mysql_query("SELECT * FROM wp_postmeta WHERE meta_key LIKE 'etn_plant_family' AND meta_value LIKE '$term->slug'");
// initialize counter to check for any published posts under this term
$etn_actual_cnt=0;
// if there are no entries, add the term_taxonomy_id to the exclude list
if (mysql_affected_rows()==0)
{
$etn_exlude_these=$etn_exlude_these.$term->term_taxonomy_id.",";
}else{
// if not check for entries
while ($getting_empties=mysql_fetch_array($etn_chk_empty))
{
$etn_check_actual=mysql_query("SELECT * FROM wp_posts WHERE ID=".$getting_empties['post_id'] );
// if there are no entries, again, add the term_taxonomy_id to the exclude list
if (mysql_affected_rows()==0)
{
$etn_exlude_these=$etn_exlude_these.$term->term_taxonomy_id.",";
}else{

while ($fetching_rec=mysql_fetch_array($etn_check_actual))
{
// if there is at least one “published” post under this term, increment the counter
if ($fetching_rec[‘post_status’]==”publish”)
{
$etn_actual_cnt=$etn_actual_cnt+1;
break;
}

}
// if the while loop finishes and returns a “0” value with the counter, which means the entries were “drafts”, again add the term_taxonomy_id to the exclude list
if ( $etn_actual_cnt==0)
{
$etn_exlude_these=$etn_exlude_these.$term->term_taxonomy_id.”,”;
}
}

}
}
}
// clean up the “,” at the end of the exclude list
if (substr ($etn_exlude_these,strlen($etn_exlude_these)==”,”))
{
$etn_exlude_these=substr($etn_exlude_these,0,(strlen($etn_exclude_these)-1));
}
// redo the get_terms query, this time with the exclude list.
$terms = get_terms( ‘family’, array(‘orderby’ => ‘name’,’hide_empty’ => 0,’exclude’ => $etn_exlude_these) );
$wp_query = new WP_Query ($terms);
/////————————————–>>>>

Once again, this may be an old-school way of solving this.. and there may be a more straightforward way of doing this or there may even be a combination of WordPress functions to simplify the process.

Back To Top