Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.