Tagged: related posts
- This topic has 9 replies, 3 voices, and was last updated 5 years, 11 months ago by
Jason.
-
AuthorPosts
-
-
February 23, 2016 at 8:29 am #5986
wpmusketeerMemberHi there,
I’m trying to get my head round the way post to post relationships work in Piklist. Here’s what I’ve got set up currently:
- A jobs CPT.
- A contacts CPT.
When editing a Job I have added a field to relate it to a Contact. The Job can only belong to one Contact. The field code looks like this:
piklist( 'field', array( 'type' => 'select', 'field' => 'related_contact', 'label' => __( 'Contact', 'core-functionality' ), 'relate' => array( 'scope' => 'contact', ), 'choices' => array( 'none' => __( 'Choose a Contact', 'core-functionality' ) ) + piklist( get_posts( array( 'post_type' => 'contact', 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC', ) ), array( 'ID', 'post_title' ) ), ) );This seems to work fine. The ID of the contact is correctly saved in the ‘related_contact’ field.
Now, when editing a Contact, I would like to pull in the list of related Jobs. However, I can’t seem to get this to work. I’ve tried it the Piklist way:
$related = get_posts( array( 'post_type' => 'job', 'posts_per_page' => -1, 'post_belongs' => $post->ID, 'suppress_filters' => false, ) ); echo var_dump( $related );I’ve tried this with ‘post_belongs’ and ‘post_has’ and in both cases $related is an empty array.
I’ve also tried it the WordPress way:
$related = get_posts( array( 'post_type' => 'job', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => 'related_contact', 'value' => $post->ID, ), ), 'suppress_filters' => false, ) ); echo var_dump( $related );But in that instance I also get an empty $related array.
Am I missing something here?
Thanks for your time.
Dave
-
February 23, 2016 at 10:07 am #5988
wpmusketeerMemberHmm… I’ve done some more testing.
I’ve added the following on the edit page of a Job this time:
$related = get_posts( array( 'post_type' => 'contact', 'posts_per_page' => -1, 'post_belongs' => $post->ID, 'suppress_filters' => false, ) ); echo var_dump( $related );This still doesn’t work, but when I change ‘post_belongs’ to ‘belongs_to’ (which I did accidentally because I have Rails syntax ingrained in my brain) it worked! I haven’t seen this documented anywhere so I’m pretty perplexed.
However, even ‘belongs_to’ doesn’t work on the Contact edit page. The above is redundant on the Job edit page because I’m already able to see the attached Contact in its relevant metabox. I really need to see the list of jobs attached to a Contact when on the Contact edit page.
-
February 23, 2016 at 11:05 am #5989
wpmusketeerMemberOK. Sorted it.
On the Contact edit page I had to supply
'post_status' => 'any'. I assumed not supplying a ‘post_status’ parameter would be the same thing. And indeed, on the Job edit page it seems to work fine (my Job CPT has custom statuses, perhaps that’s why). But on the Contact CPT it doesn’t work without ‘post_status’ supplied. -
February 23, 2016 at 11:07 am #5990
wpmusketeerMemberAlthough it still only works with ‘belongs_to’ not ‘post_belongs’…
-
February 23, 2016 at 11:22 am #5992
wpmusketeerMemberDoh (_8(|)
Nope, it’s not working. On the edit Job page it is returning ALL Contacts (even ones that aren’t related) and on the edit Contact page it’s returning ALL Jobs (even ones that aren’t related).
🙁
-
February 28, 2016 at 10:18 pm #6006
SteveKeymaster@wpmusketeer– Welcome to the Piklist community!
Try updating to the latest beta and let us know if it works better for you.
-
February 29, 2016 at 7:57 am #6014
wpmusketeerMemberHi Steve,
Thanks for getting back to me. I’m already on the version you linked to. I copy pasted the downloaded directory anyway just to be sure.
Still no success 🙁
Do you have to define a relate field on both CPTs? Currently I’m only defining the relationship on the Job.
Can you tell me what changes I should be seeing in the database so I can better understand what’s supposed to happen and what’s not happening currently?
Cheers,
Dave
-
March 1, 2016 at 11:45 am #6020
JasonKeymasterHey @wpmusketeer !
Sorry you’re having issues with the relate code. I use it all the time in my projects, so I can assure you it does work. I also wrote a lot of the internal Piklist relate code by this point, so hopefully I can help you get this sorted. 🙂
To help you debug, how the relate works is that when you perform a query is grabs all the IDs that the object is related to and limits the results to those IDs. It’s pretty straightforward, so every other aspect of the queries (e.g. post_status) should work as expected. I have very complex queries using post_has and post_belongs, so I’m confident it’s working correctly.
Also, to help you understand, you only need a single relate field to establish a bi-directional relationship. Whatever (in this case) post has the field also has the other post, conversely the other post belongs to the post.
All that being said, I believe your issue is that, in your field, you have:
'relate' => array( 'scope' => 'contact' )The scope should be
postnotcontact. I assumecontactis your post type, but understand that the object relationships don’t care what the post type is, they just care whether it’s a post, user, or comment. So the scope would only ever be one of those three, depending on what you’re relating to.Hope this helps!
-
March 1, 2016 at 6:56 pm #6023
wpmusketeerMemberPenny. Dropped. 🙂
It appears to be working now. Thanks so much Jason!
-
March 1, 2016 at 7:56 pm #6024
JasonKeymasterAwesome! You got it! 🙂
-
-
AuthorPosts
- The topic ‘Access related posts from current post’ is closed to new replies.