WordPress.org

Support

Support » Plugins and Hacks » [Resolved] [Plugin: Posts 2 Posts] Filtering connections by author

[Resolved] [Plugin: Posts 2 Posts] Filtering connections by author

  • Hi,

    I am trying to connect two custom post types to one another. Let’s call them A and B.

    When someone is adding a post of type A, I want him to be able to connect it to a B post type, but I want to give him a list of B post types that belong only to the same author as that who wrote A itself.

    How can I achieve that?

    http://wordpress.org/extend/plugins/posts-to-posts/

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author scribu

    @scribu

    You can use the ‘p2p_connectable_args’ filter:

    https://github.com/scribu/wp-posts-to-posts/wiki/Admin-box-filters

    Hi,

    I tried but it doesn’t work for me:

    function connection_by_author( $args, $ctype, $post_id ) {
    	if ( 'snippet_to_kids' == $ctype->name ) {
    		if (!is_admin())
    			$args['author'] = get_current_user_id();
    	}
    
    	return $args;
    }
    add_filter( 'p2p_connectable_args', 'connection_by_author', 10, 3 );

    It still shows a “search box” for the posts, and when I ask it to show a list, it just shows everyone in the list.

    Plugin Author scribu

    @scribu

    Are you sure it’s running? That !is_admin() check looks suspicious.

    You are somewhat correct…
    When I remove it, then things change – but now I see the wrong post types there – I see the list of posts of the author and not the list of kids.

    And another thing – is there a way to show them there automatically instead of having to press on “View all”? I expect the list to be short.

    Plugin Author scribu

    @scribu

    Please paste your ‘snippet_to_kids’ connection type definion in a http://pastebin.com

    Plugin Author scribu

    @scribu

    You should not need to set the post type yourself; it’s set automatically.

    You might want to check for the direction, though, so that it only affects “snippet” or “kid”, not both:

    if ( 'snippet_to_kids' == $ctype->name && 'from' == $ctype->direction )

    If that doesn’t work, replace ‘from’ with ‘to’.

    Didn’t work. Not with ‘from’ and not with ‘to’.
    I still get the full list of all kids…

    Plugin Author scribu

    @scribu

    So, what happens when you use this exact piece of code:

    function connection_by_author( $args, $ctype, $post_id ) {
    	if ( 'snippet_to_kids' == $ctype->name ) {
    		$args['author'] = get_current_user_id();
    	}
    
    	return $args;
    }
    add_filter( 'p2p_connectable_args', 'connection_by_author', 10, 3 );

    Note that it will only work if the user is logged in.

    I am dealing only with logged in users here – this is all done from the backend admin panels.

    When I use it the way you wrote it above, I get the list of snippets instead of the list of kids, and they are those related only to the author himself.

    Plugin Author scribu

    @scribu

    Ok, I set up a similar environment and it works fine for me.

    Are you sure you don’t have some other code interfering with WP_Query, perhaps hooked to ‘pre_get_posts’ or ‘parse_query’?

    Yap… that was the issue.
    I had a too-encopassing pre_get_posts filter.
    Fixed that – thanks!

    One last issue – is there a way to skip the “View all” button and just open up the list in advance?

    Plugin Author scribu

    @scribu

    Yep, it’s already done in the development version (1.4-alpha).

    Can’t wait for it to be released 🙂
    I’ll check out the alpha version to see how well it works for me.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘[Resolved] [Plugin: Posts 2 Posts] Filtering connections by author’ is closed to new replies.