Skip to content

Instantly share code, notes, and snippets.

@ajaydsouza
Last active December 1, 2022 20:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajaydsouza/968b24a052e858bf8926 to your computer and use it in GitHub Desktop.
Save ajaydsouza/968b24a052e858bf8926 to your computer and use it in GitHub Desktop.
Contextual Related Posts plugin API examples
<?php
/*
* This example fetches the related posts for the current post ID and displays the output in a custom format restricted by the 'news' category
*
*/
if ( function_exists( 'get_crp_posts_id' ) ) {
global $post;
$scores = get_crp_posts_id( array(
'postid' => $post->ID,
'limit' => 7
) );
$posts = wp_list_pluck( (array) $scores, 'ID' );
$args = array(
'post__in' => $posts,
'posts_per_page' => 7,
'category_name' => 'news',
'ignore_sticky_posts' => 1
);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
echo '<a href="' . get_permalink( get_the_ID() ) . '">';
the_title();
echo '</a>';
wp_reset_postdata();
}
} else {
}
wp_reset_query();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment