Skip to content

Instantly share code, notes, and snippets.

@pickplugins
Created March 12, 2024 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pickplugins/0fdd932280cdebebca06aff08c1048df to your computer and use it in GitHub Desktop.
Save pickplugins/0fdd932280cdebebca06aff08c1048df to your computer and use it in GitHub Desktop.
Post Grid - Related post by current post categories
function post_grid_query_args_20200429($query_args, $args)
{
$current_post_id = get_the_ID();
$taxonomy = "category";
$post_categories = get_the_terms($current_post_id, $taxonomy);
$tax_query = isset($query_args['tax_query']) ? $query_args['tax_query'] : [];
$term_ids = [];
foreach ($post_categories as $category) {
$term_id = isset($category->term_id) ? $category->term_id : '';
if (!empty($term_id)) {
$term_ids[] = $term_id;
}
}
if (!empty($term_ids)) {
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term_ids,
'operator' => "IN",
);
}
$extra_query = array(
'tax_query' => $tax_query,
);
return array_merge($query_args, $extra_query);
}
add_filter('post_grid_query_args', 'post_grid_query_args_20200429', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment