Advertisement
Twansparant

Display Posts 2 Posts Admin metabox only on specific page ID

Aug 31st, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. // Define page-post-type connection
  2. p2p_register_connection_type( array(
  3.     'name'      => 'page-post-type',
  4.     'from'      => 'page',
  5.     'to'        => 'post-type',
  6.     'sortable'  => true,
  7.     'cardinality'   => 'one-to-many',
  8.     'admin_box'     => array(
  9.         'show'      => 'from',
  10.         'context'   => 'side'
  11.     ),
  12.     'admin_column'  => false,
  13.     'title'     => __('Connected post types on page', 'domain')
  14. ) );
  15.  
  16. // Show Admin Box for page-post-type connection only on homepage
  17. function restrict_p2p_box_display( $show, $ctype, $post ) {
  18.     if ( 'page-post-type' == $ctype->name) {
  19.         $frontpage_id = get_option('page_on_front');
  20.         return($post->ID == $frontpage_id);
  21.     }
  22.     return $show;
  23. }
  24. add_filter( 'p2p_admin_box_show', 'restrict_p2p_box_display', 10, 3 );
  25.  
  26.  
  27. // Show page-post-type connections in homepage template
  28. if ( is_front_page() ) :               
  29.     if (function_exists('p2p_type')) {
  30.         $connected = new WP_Query( array(
  31.             'post_type' => 'post-type',
  32.             'connected_type' => 'page-post-type',
  33.             'connected_items' => get_queried_object(),
  34.             'nopaging' => true
  35.         ) );
  36.         if ($connected->have_posts() ) :
  37.             while ($connected->have_posts() ) : $connected->the_post();
  38.                 the_title();
  39.             endwhile;
  40.         endif;
  41.         wp_reset_postdata();
  42.         wp_reset_query();
  43.     } else {
  44.         echo 'Plugin not found';
  45.     }
  46. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement