WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Filtering connections by author (15 posts)

  1. tsahil
    Member
    Posted 1 year ago #

    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/

  2. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    You can use the 'p2p_connectable_args' filter:

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

  3. tsahil
    Member
    Posted 1 year ago #

    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.

  4. scribu
    Member
    Plugin Author

    Posted 1 year ago #

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

  5. tsahil
    Member
    Posted 1 year ago #

    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.

  6. scribu
    Member
    Plugin Author

    Posted 1 year ago #

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

  7. tsahil
    Member
    Posted 1 year ago #

  8. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    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'.

  9. tsahil
    Member
    Posted 1 year ago #

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

  10. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  11. tsahil
    Member
    Posted 1 year ago #

    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.

  12. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    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'?

  13. tsahil
    Member
    Posted 1 year ago #

    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?

  14. scribu
    Member
    Plugin Author

    Posted 1 year ago #

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

  15. tsahil
    Member
    Posted 1 year ago #

    Can't wait for it to be released :-)
    I'll check out the alpha version to see how well it works for me.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.