[Plugin: Posts 2 Posts] duplicates using p2p_get_connected
-
The single page contains a product. What I’m trying to accomplish with posts2posts is to have it relate to the accessories for this product as well as show related products. Both of these are custom posts.
The problem that I’m having is that when using p2p_get_connected it doesn’t filter the results and I always get both accessories and products.
I have tried all types of combinations but have had no success. An additional complication is that the custom post product relates to itself, so that I can get the related products.
Some help to steer me in the right direction would be greatly appreciated.
My code in the functions.php file is as follows:
add_action('init', 'products_acccessories_init', 100); function products_acccessories_init() { register_post_type('products', array( 'label' => 'Products', 'public' => true, 'show_ui' => true, 'query_var' => 'products', 'rewrite' => array('slug' => 'products'), 'hierarchical' => false, 'prevent_duplicates' => false, //'supports' => array('title','editor','custom-fields'), ) ); register_post_type('products_accessories', array( 'label' => 'Products Accessories', 'public' => true, 'show_ui' => true, 'query_var' => 'products_accessories', 'rewrite' => array('slug' => 'products_accessories'), 'hierarchical' => true, 'prevent_duplicates' => false, //'supports' => array('title','editor','custom-fields'), ) ); if ( !function_exists('products_acccessories_init') ) return; p2p_register_connection_type( 'products_accessories', 'products' ); p2p_register_connection_type( 'products', 'products' ); global $wp_rewrite; $wp_rewrite->flush_rules(false); // This only needs be done first time }
and my code in the single-product.php page is:
<?php $upload_dir = wp_upload_dir(); if (count($accesories = p2p_get_connected(get_the_ID(),'from', 'products_accessories'))): //looks up in the post to post table the accessories related to the product. Setup in the ups product admin page foreach($accesories as $accesory_id): $accesory = get_post($accesory_id);?> <div class="accessory_item_wrapper"> <div class="accessory_thumbnail"> <?php if (get_the_post_thumbnail($accesory->ID, 'thumbnail')):?> <a href=" <?php echo get_permalink($accesory->ID); ?> "> <?php echo get_the_post_thumbnail($accesory->ID, 'thumbnail'); ?> </a> <!-- if no picture insert logo --> <?php else:?> <a href="<?php echo get_permalink($accesory->ID); ?>"> <img src="<?php echo $upload_dir['baseurl'].'/2010/11/product_blank_background-150x150.png'; ?>" alt="<?php echo get_permalink($accesory->ID); ?>" width='150' height='150' /> </a> <?php endif;?> </div> <!-- end accessory_thumbnail --> </div> <!-- end accessory_item_wrapper --> <?php endforeach; else:?> <div class="description_accessory_wrapper"> <?php echo "there are no accessories associated to this product"; ?> </div> <!-- end description_accessory_wrapper --> <?php endif;?>
Thanks a lot for the plug-in but it escapes me what I’m missing.
- The topic ‘[Plugin: Posts 2 Posts] duplicates using p2p_get_connected’ is closed to new replies.