Contextual Related Posts v3.0.0 introduced CRP_Query which works as a wrapper for WP_Query. This brings all the power and flexibility of WP_Query to Contextual Related Posts. If you’re not familiar with WP_Query, I recommend reading docs.
Standard Loop
<?php
// The Query.
$the_query = new CRP_Query( $args );
// The Loop.
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found.
}
/* Restore original Post Data */
wp_reset_postdata();
get_crp_posts()
get_crp_posts() works as a wrapper to CRP_Query and can be used to retrieve an array of the related posts. It also accepts the same $args
as CRP_Query above.
Parameters
In addition to the WP_Query parameters, CRP_Query also takes these additional parameters.
$args
(array) (Optional) Arguments to retrieve posts. See crp_get_registered_settings()
for all available arguments.
- ‘postid’
(int) Get related posts for a specific post ID. If you are using CRP_Query or get_crp_posts() outside the loop, then you will need to pass this else you might get errors when trying to fetch the related posts. - ‘include_cat_ids’
(array|string) An array or comma-separated string of category IDs. This should be the term_taxonomy_id. - ‘include_post_ids’
(array|string) An array or comma-separated string of post IDs. - ‘offset’
(int) number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. The'offset'
parameter is ignored when'posts_per_page'=>-1
(show all posts) is used. - ‘strict_limit’
(bool) If this is set to false, then it will fetch 3x posts.