The website sells sheet music publications. It has a number of ACF fields, one is named Series and all publications in the same series will have the same value in this field. On the Single Product Page template I’ve placed a short code (though eventually I want that to be in a button) that takes the value from the Series field and then displays the titles of all posts that have the same value. The code I have for this is below but doesn’t display the information required.
`function display_related_titles_by_series() {
// Get the current post’s Series value (product description page)
$series_value = get_field(‘series’);
echo $series_value;
// Query posts with the same Series value
$args = array(
'post_type' => 'product', // WooCommerce
'meta_key' => 'series',
'meta_value' => $series_value,
);
$related_posts = new WP_Query($args);
// Display the list of titles
if ($related_posts->have_posts()) {
echo '<ul>';
while ($related_posts->have_posts()) {
$related_posts->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
wp_reset_postdata();
} else {
echo 'No related titles found.';
}
}
add_shortcode(‘related_titles’, ‘display_related_titles_by_series’);`
As far as I can see this should be OK but nothing is being displayed, not even the No Related Titles Found message, when the page loads.