Custom Field Suite – Fix For Archive Pages

If you are using Custom Field Suite to create groups of fields for posts AND any of those fields in the group uses taxonomy terms, you may find that your archive pages are not displaying any posts when filtered by taxonomy.

This is because Custom Field Suite stores the taxonomy post data within post_meta fields rather than updating the WordPress taxonomy relationship table.

You will need to modify your wp_query arguments to search for posts based on the values in the post_meta value field that match CFS key value.

In the example below, we have 3 custom taxonomies and need to convert them to meta_key values based on the field label name we gave them in Custom Field Suite.

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$qobj = get_queried_object();
 //var_dump($qobj); // debugging only

 //echo '<br><br>taxonomy ' . $qobj->taxonomy;  // debugging only
 //echo '<br><br>taxonomy ' . $qobj->term_id; // debugging only
 //  Convert taxonomy to its CFS equivalent field name
 if ($qobj->taxonomy == 'cuisine') {
 	$meta_key = 'cfs_cuisine';
 }
 if ($qobj->taxonomy == 'course') {
 	$meta_key = 'cfs_course';
 }
 if ($qobj->taxonomy == 'ingredient') {
 	$meta_key = 'cfs_ingredient';
 }

// concatenate the query
$args = array(
  'post_type'         => 'post',
  'post_status'       => 'publish',
  'posts_per_page' => 6,
  'order'             => 'ASC',
  'paged' => $paged,
  // must use meta key from CFS
  'meta_key'	=>  $meta_key, // the CFS equivalent field name of the taxonomy
  'meta_value'	=> $qobj->term_id,  // must be taxonomy term id
  'meta_compare'	=>  '=',
);

$uwp_query = new WP_Query( $args );
//echo '<pre>'; // debugging only
//var_dump($uwp_query_query); // debugging only // debugging only
//echo 'query ' . $random_query->request; // debugging only
// echo '</pre>'; // debugging only
echo 'Total Posts: ' . $uwp_query->found_posts . '<br>';

if ($uwp_query->have_posts()) {
  while (uwp_query->have_posts()) {
    $uwp_query->the_post();

        echo '<div class="post">';
        echo the_post_thumbnail( 'no-size', array( 'alt' => get_the_title() ) );
        echo '<h1><a href="' . get_permalink() . '">' . get_the_title() . '</a></h1>';
        the_excerpt(). '...';
        edit_post_link(__('Edit This'));
        echo '</div>';
    }
  } else {
    echo 'sorry nothing found under ';
    single_cat_title();
  }
  wp_reset_postdata();

 

Available for Amazon Prime