Created
March 12, 2024 07:22
-
-
Save pickplugins/0fdd932280cdebebca06aff08c1048df to your computer and use it in GitHub Desktop.
Post Grid - Related post by current post categories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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