Skip to content

Instantly share code, notes, and snippets.

@keesiemeijer
Created September 2, 2017 14:13
Show Gist options
  • Save keesiemeijer/5f053e2d6a858774c34acddfc768ba12 to your computer and use it in GitHub Desktop.
Save keesiemeijer/5f053e2d6a858774c34acddfc768ba12 to your computer and use it in GitHub Desktop.
Override related posts with the WordPress Related Posts by Taxonomy plugin
add_action( 'widget_display_callback', 'page_widget_output', 10, 3 );
function page_widget_output( $instance, $widget, $args ) {
$plugin = function_exists( 'km_rpbt_plugin' ) ? km_rpbt_plugin() : false;
// Check if the plugin is activated and this is the related posts widget.
if ( ! $plugin || ( 'Related Posts By Taxonomy' !== $args['widget_name'] ) ) {
return $instance;
}
// Get all options for the widget.
$instance = array_merge( $plugin->get_default_settings( 'widget' ), $instance );
// Here we can use our own query for related posts.
$query_args = array(
'posts_per_page' => 5,
// If you're showing thumbnails query use this meta query
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS',
),
),
);
// Get our own related posts to display with the widget
$posts = get_posts( $query_args );
// Echo the widget
$widget->widget_output( $posts, $instance, $args );
// Important to return false as we have already displayed the widget.
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment