Skip to content

Instantly share code, notes, and snippets.

@giucu91
Created September 15, 2022 09:52
Show Gist options
  • Save giucu91/01f532f3f4038ca40390a6d172dd5328 to your computer and use it in GitHub Desktop.
Save giucu91/01f532f3f4038ca40390a6d172dd5328 to your computer and use it in GitHub Desktop.
<?php
public function add_query_args__current( $args, $atts ) {
/*
* The query does not change if the current post type or the specific post is excluded.
*/
if ( wpmtst_assignment_is_post_excluded( get_the_ID() ) ) {
return $args;
}
/*
* Single post or page
*/
if ( is_single() || is_page() ) {
$args['meta_key'] = 'strong_assignment';
$args['meta_value'] = get_the_ID();
return $args;
}
/*
* Archives
*/
if ( ( is_post_type_archive() || is_category() || is_tax() || is_home() ) && in_the_loop() ) {
$args['meta_key'] = 'strong_assignment';
$args['meta_value'] = get_the_ID();
return $args;
}
if ( ( is_post_type_archive() || is_category() || is_tax() || is_home() ) && ! in_the_loop() ) {
// get the IDs of all posts being displayed
global $posts;
$post_list = array();
foreach ( $posts as $post_object ) {
$post_list[] = $post_object->ID;
}
$args['meta_key'] = 'strong_assignment';
$args['meta_value'] = join( ',', $post_list );
$args['meta_compare'] = 'IN';
return $args;
}
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment