I am using two loops on a page, one is grouped posts using 'for each' and one is not.
They both use the same facetwp query which is:
<?php
return [
"post_type" => [
"prime-ministers"
],
"post_status" => [
"publish"
],
"meta_query" => [
"sort_0" => [
"key" => "pm_century",
"type" => "CHAR"
],
"sort_1" => [
"key" => "serving_period_1_start",
"type" => "CHAR"
]
],
"orderby" => [
"sort_0" => "DESC",
"sort_1" => "DESC"
],
"posts_per_page" => -1
];
It's a custom post type called prime-ministers and I am trying to display the value of a custom taxonomy called 'party' - but I only want to show the Yoast Primary category
For some reason, it shows no problem in the regular ungrouped loop like so:
<?php $post_type = get_post_type();
$id = get_the_ID();
$taxonomy = current( get_object_taxonomies( $post_type, 'objects' ) )->name ?? false;
$primary_term_id = function_exists( 'tsf' ) ? tsf()->get_primary_term( $id, $taxonomy ) : 0;
$primary_term_id = $primary_term_id ?: ( get_the_terms( $id, $taxonomy )[0]->term_id ?? null );
if ( $primary_term_id ) {
$term = get_term( $primary_term_id, $taxonomy ); ?>
<p class="main-listing-party"><?php echo $term->name ?? '';?> </p>
<?php } ?>
But it does not work (it displays nothing at all) in the 'for each' loop which is this:
<?php
$century_posts = array();
while ($query->have_posts()) {
$query->the_post();
$century = get_field('pm_century', get_the_id());
$century_posts[$century][] = $post;
}
wp_reset_query();
foreach ($century_posts as $century_post => $century_title) {
?>
<!-- Display the century -->
<div class="century-line-wrap">
<h2 class="century-name"><?php echo esc_html($century_post); ?></h2>
<div class="century-line"></div>
</div>
<?php
foreach ($century_title as $listing => $single_listing) {
setup_postdata($single_listing);
$post_id = $single_listing->ID;
$title = get_the_title($post_id);
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'large' );
?>
<?php $nameclass = str_replace(" ", "", $title); ?>
<div class="single-prime-minister <?php echo strip_tags(get_the_term_list( $post_id, 'party', ' ',', ')); ?> <?php echo $nameclass ?>">
<a href="<?php the_permalink($post_id); ?>" title="<?php echo $title ?>" aria-label="Find out more about <?php echo $title ?>">
<div class="pm-filter-profile-image">
<img src="<?php echo $image[0]; ?>" alt="" />
</div>
</a>
<div class="pm-filter-details-wrap">
<h4><?php echo get_the_title(); ?></h4>
<?php $post_type = get_post_type();
$id = get_the_ID();
$taxonomy = current( get_object_taxonomies( $post_type, 'objects' ) )->name ?? false;
$primary_term_id = function_exists( 'tsf' ) ? tsf()->get_primary_term( $id, $taxonomy ) : 0;
$primary_term_id = $primary_term_id ?: ( get_the_terms( $id, $taxonomy )[0]->term_id ?? null );
if ( $primary_term_id ) {
$term = get_term( $primary_term_id, $taxonomy ); ?>
<p class="main-listing-party"><?php echo $term->name ?? '';?> </p>
<?php } ?>
</div>
</div>
<?php
}
wp_reset_postdata();
} ?>
I also tried this:
<?php
$primary_term_id = yoast_get_primary_term_id( $post_id, 'party' );
?>
<p><?php echo $primary_term_id ?></p>
And
<?php $post_type = get_post_type();
$taxonomy = current( get_object_taxonomies( $post_type, 'objects' ) )->name ?? false;
$primary_term_id = function_exists( 'tsf' ) ? tsf()->get_primary_term( $post_id, $taxonomy ) : 0;
$primary_term_id = $primary_term_id ?: ( get_the_terms( $post_id, $taxonomy )[0]->term_id ?? null );
if ( $primary_term_id ) {
$term = get_term( $primary_term_id, $taxonomy ); ?>
<p class="main-listing-party"><?php echo $term->name ?? '';?> </p>
<?php } ?>
Nothing I do seems to work
Thanks
In the end, I got this working with