About 'checking specific connections' – conditional if user has connection
-
Hi, sorry if this is a bit of a ‘noob’ question, I thought I had this figured out but need a little direction.
Basically let’s say I have a connection called users_to_posts and in a template in my theme I want to display some code only if the currently viewing user has a connection to a post (not a specific post, just any post. I believe for specific posts you can just use the post ID as the post_a or post_b below)
So, using the example on the wiki:
https://github.com/scribu/wp-posts-to-posts/wiki/Checking-specific-connections<?php $p2p_id = p2p_type( 'YOUR_CONNECTION_TYPE' )->get_p2p_id( $post_a, $post_b ); if ( $p2p_id ) { // connection exists $connection_field = p2p_get_meta( $p2p_id, 'YOUR_FIELD_KEY', true ); var_dump( $connection_field ); } else { // connection doesn't exist }
I am trying the code below but it’s not right. I’m not sure I’m calling the current user properly or how to include the custom post type name (in this example it’s just ‘post’ but I will need to know how to change this to a custom post type name also)
<?php $user_ID = get_current_user_id(); $p2p_id = p2p_type( 'users_to_posts' )->get_p2p_id( $user_ID, 'post' ); if ( $p2p_id ) { // connection exists echo "I will echo something here"; } else { // connection doesn't exist }
Can anyone advise? Many thanks
- The topic ‘About 'checking specific connections' – conditional if user has connection’ is closed to new replies.