PenciDesign Supporter replied
Hi,
You should paste this code into the function.php of the child theme or via the CodeSnippet plugin here: https://wordpress.org/plugins/code-snippets/
/**
* Combine category posts with posts tagged with tag ID 4
* Works on category archive pages only.
*/
add_action( 'pre_get_posts', function( $query ) {
// Only modify the main frontend category archive query
if (
! is_admin()
&& $query->is_main_query()
&& $query->is_category()
) {
// Get current category ID
$category = get_queried_object();
$category_id = $category->term_id ?? 0;
// Combine category + tag=4
$tax_query = [
'relation' => 'OR',
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $category_id,
],
[
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => [4],
],
];
// Set the new tax query
$query->set( 'tax_query', $tax_query );
// Ensure posts are ordered by date (newest first)
$query->set( 'orderby', 'date' );
$query->set( 'order', 'DESC' );
// Optional: define posts per page if needed
// $query->set( 'posts_per_page', 10 );
}
});
/**
* Remove duplicate posts if they appear in both category & tag.
*/
add_filter( 'posts_distinct', function( $distinct, $query ) {
if (
$query->is_main_query()
&& $query->is_category()
) {
return 'DISTINCT';
}
return $distinct;
}, 10, 2 );
Replace [4] with the tag ID that you want to show.
Regards,
PenciDesign.