Skip to content

Instantly share code, notes, and snippets.

@cdillon
Created March 14, 2018 21:13
Show Gist options
  • Save cdillon/81b0282d34907e392b2ec0abe141f070 to your computer and use it in GitHub Desktop.
Save cdillon/81b0282d34907e392b2ec0abe141f070 to your computer and use it in GitHub Desktop.
Don't repeat testimonials on the same page with multiple views.
<?php
/**
* Set global variable to track posts.
*/
function no_repeat_init() {
global $testimonials_showing;
$testimonials_showing = array();
}
add_action( 'wp', 'no_repeat_init' );
/**
* Add query argument to exclude posts already showing.
*
* @param array $args
*
* @return array
*/
function no_repeat_query_args( $args ) {
global $testimonials_showing;
$args['post__not_in'] = $testimonials_showing;
return $args;
}
add_filter( 'wpmtst_build_query', 'no_repeat_query_args' );
/**
* Track posts after being shown.
*/
function no_repeat_get_query() {
global $testimonials_showing;
$query = WPMST()->get_query();
foreach ( $query->posts as $post_object ) {
$testimonials_showing[] = $post_object->ID;
}
}
add_action( 'wpmtst_view_rendered', 'no_repeat_get_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment