WordPress Related Posts: Using WordPress Toolset and WP Posts to Posts – Shortcode Alert

Four Eyes PHP, Web Design, Web Development, Wordpress, WP-Toolset

views-logo1If you read my previous post,  WordPress Custom Related Posts: Toolset Plugin & Posts 2 Posts – Combining APIs for even more flexibility, I discussed how combining  Toolset with thePosts 2 Posts plugin creates an easier WordPress related posts interface for the website administrator to create content relationships.  Four Eyes is currently working on a new website for David Keil’s YogAnatomy.com and the site needed a method for connecting content types to one another in an easy and efficient manner.  While we created a solution for Single Post pages, we still needed to be able to use the code in loop while using Toolset.  The solution is a shortcode function that combines the power of the Views API with the enhanced query structure of the Post 2 Posts plugin.


/* Toolset to Post 2 Posts View Integration */
add_shortcode( 'get-related-view', 'get_related_view' );
function get_related_view( $atts ) {


extract( shortcode_atts(
array(
'my_post_id' => '', /*insert the id of the post to pull related content from typically 7490*/
'view_name' => '', /*the name of the view */
'connection_type_name' => '', /*the name of the connection type from post 2 posts*/
), $atts )
);
$my_post_id = do_shortcode($my_post_id); /* add this line to embed shortcode in shortcode in views */


$connected = new WP_Query( array(
'connected_type' => $connection_type_name,
'connected_items' => $my_post_id,
'nopaging' => true,
) );

if ( $connected->have_posts() ) {
while ( $connected->have_posts() ) {
$connected->the_post();
$thisid = $connected->post->p2p_to;
$args = array('title' => $view_name,'pid' => $thisid);
$myview = render_view( $args );
}
}


return $myview;
wp_reset_postdata();
}

Working in Toolset’s Views you can drop the shortcode into a standard view or a content template:
[get-related-view my_post_id='7490' view_name='Event Destinations' connection_type_name='event_locales']

Create a View in Toolset and a Post Relationship in Post 2 Posts as you normally would – use the shortcode and you have the best of both worlds.

Knowledge of the Posts 2 Posts plugin and Toolset API is paramount to using this function.  Contact Four Eyes and we are happy to work with you!

 

Four Eyes is a Toolset Certified Partner & Consultant
Contact us to learn more »