scribu / wp-posts-to-posts

Efficient many-to-many connections between posts, pages, custom post types, users.
http://wordpress.org/plugins/posts-to-posts/
969 stars 260 forks source link

Register connection type to a specifc page #552

Open nunospot opened 6 years ago

nunospot commented 6 years ago

Hello,

Is there a way to register a post type to a specific template page?

Example:

function connection() { p2p_register_connection_type( array( 'name' => 'connection', 'from' => get_page_template('the_template.php'), //the page template 'to' => 'the_post_type' ) ); }

Connect a post type "the_post_type" to the pages that have the "the_template.php" template?

Thank you.

kkerley commented 6 years ago

Did you ever end up figuring out if this was possible? I just inherited a project using this plugin and need to accomplish something similar.

nunospot commented 6 years ago

No. This was a "nice to have" thing :\

chrisgherbert commented 6 years ago

I don't think you can restrict the actual connection to a specific page template, but you can only show the p2p interface on certain templates using the p2p_admin_box_show filter: https://github.com/scribu/wp-posts-to-posts/wiki/Actions-and-filters

<?php
function restrict_p2p_box_display( $show, $ctype, $post ) {
    if ( 'posts_to_pages' == $ctype->name && 'to' == $ctype->get_direction() ) {
        return ( 'YOUR-TEMPLATE.php' == $post->page_template );
    }

    return $show;
}

add_filter( 'p2p_admin_box_show', 'restrict_p2p_box_display', 10, 3 );