tsahil
Member
Posted 12 months 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/
tsahil
Member
Posted 11 months 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.
Are you sure it's running? That !is_admin() check looks suspicious.
tsahil
Member
Posted 11 months 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.
Please paste your 'snippet_to_kids' connection type definion in a http://pastebin.com
tsahil
Member
Posted 11 months 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'.
tsahil
Member
Posted 11 months ago #
Didn't work. Not with 'from' and not with 'to'.
I still get the full list of all kids...
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.
tsahil
Member
Posted 11 months 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.
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'?
tsahil
Member
Posted 11 months 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?
Yep, it's already done in the development version (1.4-alpha).
tsahil
Member
Posted 11 months ago #
Can't wait for it to be released :-)
I'll check out the alpha version to see how well it works for me.