Skip to content

Instantly share code, notes, and snippets.

@jeherve
Created February 28, 2014 18:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeherve/9276637 to your computer and use it in GitHub Desktop.
Save jeherve/9276637 to your computer and use it in GitHub Desktop.
[Jetpack] Related Posts: retrieve Related Posts manually, and return post titles
<?php
function jeherve_custom_related( $atts ) {
$posts_titles = array();
if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) {
$related = Jetpack_RelatedPosts::init_raw()
->set_query_name( 'jeherve-shortcode' ) // Optional, name can be anything
->get_for_post_id(
get_the_ID(),
array( 'size' => 3 )
);
if ( $related ) {
foreach ( $related as $result ) {
$related_post = get_post( $result[ 'id' ] );
$posts_titles[] = $related_post->post_title;
}
}
}
return implode( ', ', $posts_titles );
}
add_shortcode( 'jprel', 'jeherve_custom_related' );
@joyfully
Copy link

Working grate. Is there a way to return posts url adress instead of post titles, names, date, etc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment