Plugin Author
scribu
(@scribu)
First of all, does the product link change depending on which client the product is connected to? If not, it should be a normal product custom field:
http://codex.wordpress.org/Custom_Fields
Thread Starter
argeee
(@argeee)
Yes. The idea being that the link is a placeholder for our product at the distributor’s website. The first part of the link does not change, but the last part, the part associated with the product id does.
Thanks again
Plugin Author
scribu
(@scribu)
Ok, then p2p_get_meta() doesn’t work because your WP_Query isn’t connecting any products. It just gets a list of clients.
Assuming the code is run on single-product.php
, try using:
$wp_query = $my_connection_type->get_connected( get_queried_object_id(), array(
'client_type' => 'Distributor'
);
Otherwise, you’ll have to replace get_queried_object_id()
with whatever product id you want to connect.
Thread Starter
argeee
(@argeee)
Hi Scribu,
I have finally have been able to come back to this. However, for the life of me I can’t make it to work. The page is single-product, I the client type is a custom post with ‘distributor’ as taxonomy type.
I have turned debug on and I get:
Notice: Undefined variable: my_connection_type
Even if I have in the function.php ‘global $my_connection_type’
Using version 1.0
thanks,
mauro
Plugin Author
scribu
(@scribu)
Thread Starter
argeee
(@argeee)
Thanks, now it works. Here is the code:
In the functions.php I have included the id:
function my_connection_types() {
// Make sure the Posts 2 Posts plugin is active.
if ( !function_exists( 'p2p_register_connection_type' ) )
p2p_register_connection_type( array(
'id'=>'product_id_link',
'from' => 'product',
'to' => 'client',
'fields' => array(
'product_link' => 'link'
)
)
);
}
add_action('init', 'my_connection_types', 100);
in the single-product.php page I have (in php tags):
$wp_query = p2p_type( 'product_id_link' )->get_connected( get_queried_object_id(), array(
'client_type' => 'Distributor'));
echo '<h2>' . _e('Where to buy', 'eq_power') . '</h2>';
while ($wp_query->have_posts() ) : $wp_query->the_post();
echo p2p_get_meta($post->p2p_id, 'product_link', true);
endwhile;
wp_reset_postdata();
I hope this is of help for someone.
Mauro