Tagged: post to user, relationships
- This topic has 16 replies, 4 voices, and was last updated 5 years, 10 months ago by
stemuedendron.
-
AuthorPosts
-
-
March 14, 2016 at 9:35 am #6070
stemuedendronMemberHello,
I have a problem with post to user relationships with latest piklist version 0.9.9.7.
I can’t create a bi-directional relation.I have two custom post types:
rf_show
rf_episodeNow there are two relationships:
rf_episode to rf_show
rf_episode to userThis is the code for the metabox for rf_episode:
<?php /* Title: On Air Post Type: rf_episode */ // field for related shows piklist('field', array( 'type' => 'checkbox', 'field' => 'rf_episode_to_show', 'label' => __('Shows', 'rf'), 'relate' => array( 'scope' => 'post' ), 'choices' => piklist( get_posts(array( 'post_type' => 'rf_show', 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' )), array('ID', 'post_title') ) )); // field for related users piklist('field', array( 'type' => 'checkbox', 'field' => 'rf_episode_to_user', 'label' => __('DJs', 'rf'), 'relate' => array( 'scope' => 'user' ), 'choices' => piklist( get_users(array( 'orderby' => 'display_name', 'order' => 'ASC', )), array('ID', 'display_name') ) ));I create an episode and select related show (id 38) and user (id 2).
This is what I get (and what I expected) in the wp_postmeta table:post_id meta_key meta_value
————————————————–
338 rf_episode_to_show 38
338 rf_episode_to_user 2
38 __relate_post 338There is a bi-directional relationship between post 38 (the show) and post 338 (the created episode).
And there is a relation between post 338 and the user with id 2.Now I expected the second part of the bi-directional relationship in the wp_usermeta table, but there is no entry. Is ‘scope’ => ‘user’ wrong?
How to do this?Thanks
Steffen -
March 14, 2016 at 12:10 pm #6072
jrcreativeMemberIf I’m understanding correctly, the user scope can only hold the core user fields within WordPress.
Try
'scope' => 'user_meta' -
March 14, 2016 at 12:34 pm #6074
stemuedendronMemberThank you, but no, doesn’t work.
I think this is not the problem here because ‘scope’ is a piklist term that will be used to finally store the realtion in the corresponding meta table. As you see in my post the ‘scope’ => ‘post’ results in saving the relation correctly in the postmeta table. Piklist should do it in the same way with ‘scope’ => ‘user’ and save the data in usermeta.
-
March 14, 2016 at 8:11 pm #6078
JasonKeymasterHey @stemuedendron
You shouldn’t need the field parameter on either of them. Try removing that and see if it fixes it.
-
March 15, 2016 at 1:27 am #6080
jrcreativeMemberWhat I have learned about Piklist is:
'scope' => 'post'will allow you to store core post fields like post_title, post_status, post_content…'scope' => 'post_meta'save to post meta, and can be whatever you want since they’re custom fieldsSame concept with
useranduser_meta.I’m not as familiar with the post to post relationships, though, so I’ll bow out of this one at this point… @steve?
-
March 15, 2016 at 6:16 am #6082
stemuedendronMemberThanks for replying, but…
@Jason
Sorry, but no. I need the ‘field’ parameter. Removing the ‘field’ parameter results in no database entries at all, means nothing is stored in the database. I think you get confused with the use of the field type ‘post-relate’ where I not have to give a field parameter because its done in the definition of ‘post-relate’ in /piklist/parts/fields/post-relate.php. But since I use the field type ‘checkbox’ I have to give a ‘field’ parameter. And thats Ok and works in the post to post relationship part.
@jrcreative
No, have tried that all, no success. Notice that we talk not about the ‘scope’ parameter but the use of scope in the ‘relate’ parameter. Look at the definition of ‘post-relate’ in /piklist/parts/fields/post-relate.php:<div class="wp-tab-panel"> <?php piklist('field', array( 'type' => 'checkbox' ,'scope' => 'post_meta' ,'field' => '_' . piklist::$prefix . 'relate_post' ,'object_id' => $arguments['object_id'] ,'choices' => piklist( get_posts(array( 'post_type' => $scope ,'numberposts' => -1 ,'orderby' => 'title' ,'order' => 'ASC' )) ,array('ID', 'post_title') ) ,'relate' => array( 'scope' => 'post' ) )); ?> </div>The use of ‘scope’ => ‘post’ in the ‘relate’ parameter results in storing the relationship in the postmeta table which is the expected behavior.
Problem is that I get no entry in the usermeta table.
-
March 17, 2016 at 7:35 am #6105
stemuedendronMemberNo solution? Is there a bug tracker somewhere?
regards
Steffen -
March 17, 2016 at 11:50 am #6107
JasonKeymasterHey @stemuedendron
I believe you’re running into a change between 0.9.5.x and 0.9.9.x. Between the two versions we made it possible to turn any field into a relate field, so long as it ultimately provided an object ID, as well as opening relationships from just posts to all objects (e.g. user to post, as you’re doing).
In 0.9.5.x it was possible to have a field with
'type' => 'relate'and'field' => 'my_field'which would both store the value as meta and store the relationship. This wasn’t an intended feature, but I admittedly used it a couple times myself and it’s even something we’re considering bringing back.Now, in 0.9.9.x, relationships work different internally, which is why you’re not limited to the relate field type and can simply add the relate parameter to the field (as you’re doing). This did unfortunately “break” being able to relate and store meta at the same time, but seeing as it wasn’t something that was intentionally supported, it was considered acceptable. All that to say, keeping the
'field'parameter on a relate field now confuses things as relational fields are special. You shouldn’t need the meta as you can simply use the'user_has','user_belongs', etc., parameter in your queries.Hope this helps!
-
March 17, 2016 at 5:36 pm #6110
stemuedendronMemberThanks Jason for the answer.
It makes things clearer now for me but not clear enough (the ‘we need no field parameter’ part, later). If I understand relationships right in 0.9.9.x they are not bidirectional in the sense that piklist would make two database entries for the relationship. Instead there is only one entry in a meta table (postmeta or usermeta) which links the IDs and the use of the parameters you mention ‘user_has’,’user_belongs’, ‘post_has, ‘post_belongs’ etc.What confused me was that there were two entries in the postmeta table in my first post-to-post example and I expected the same for the post-to-user relationship. The code for this is from the demo /piklist/add-ons/piklist-demos/parts/meta-boxes/field-relate.php and there IS a field parameter.
A search for ‘relate’ => array in the piklist folder gives three results:
/piklist/add-ons/piklist-demos/parts/meta-boxes/field-relate.php
/piklist/add-ons/piklist-demos/parts/meta-boxes/field-relationship.php
/piklist/parts/fields/post-relate.phpIn field-relate.php and post-relate.php are ‘field’ parameters, in field-relationship.php not.
Now following the demo field-relationship.php I see there are 3 fields of ‘type’ => ‘group’ with no ‘field’ parameter.
Based on this here is my test post-to-user code with a field of type ‘group’ that works:
piklist('field', array( 'type' => 'group', 'scope' => 'user', 'label' => __('DJs', 'rf'), 'relate' => array( 'scope' => 'post' ), 'fields' => array( array( 'type' => 'checkbox', 'field' => 'ID', 'choices' => piklist( get_users(array( 'orderby' => 'display_name', 'order' => 'ASC' )), array('ID', 'display_name') ) ) ) ));This way I get entries in the usermeta table (links the selected user IDs to the Post ID).
But how should the code look in a non ‘group’ field type? Transforming to this does not work:
piklist('field', array( 'type' => 'checkbox', 'scope' => 'user', 'label' => __('DJs', 'rf'), 'relate' => array( 'scope' => 'post' ), 'choices' => piklist( get_users(array( 'orderby' => 'display_name', 'order' => 'ASC' )), array('ID', 'display_name') ) ));No database entries.
So an example of how to use the ‘relate’ parameter in an ordinary field would be very helpfull.
-
March 17, 2016 at 11:44 pm #6112
JasonKeymasterHey!
No problem. You’re obviously digging into it more than most and we’re glad to help clarify things.
You’re correct when you say only a single object gets the relation row added to reflect a relationship — in fact it’s the object that belongs to the other that gets the row in its meta table. But, that doesn’t mean the relationships aren’t bidirectional. When you use the
'post_has','post_belongs', etc. query vars Piklist internally splits the query into two queries, using the former to determine the object’s relationships (given the direction has/belongs). So either the owner object is in the object’s meta, or the belonging object will have the object’s id in the meta — it works either way and is quite fast.This would be an example of a multiple select field that relates two posts:
<?php /** * Title: Services Provided * Post Type: doctor * Context: side * Priority: low */ piklist('field', array( 'type' => 'select', 'template' => 'field', 'choices' => piklist(get_posts(array( 'post_type' => 'service', 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' )), array('ID', 'post_title')), 'relate' => array( 'scope' => 'post' ), 'attributes'=> array( 'multiple' => 'multiple' ) ));Let me know if that clears things up!
-
March 18, 2016 at 10:26 am #6115
stemuedendronMemberSorry, no. Does not work. Get no database entries. This is the full code:
1. post types in own pluginadd_filter('piklist_post_types', 'rf_post_types'); function rf_post_types($post_types) { $post_types = array_merge($post_types, array( // post type for shows: 'rf_show' => array( 'title' => 'enter show name...', 'menu_icon' => plugins_url('radiofrei/images/rf-menu-icon.png'), // labels 'labels' => array( 'name' => 'Shows', 'singular_name' => 'Show' ), // wordpress features 'supports' => array( 'title', 'editor', 'revisions' ), // columns in admin 'edit_columns' => array( 'title' => 'Shows' ), // frontend - backend 'public' => true, // slug 'rewrite' => array( 'slug' => 'shows' ) ), // post type for episodes: 'rf_episode' => array( 'title' => 'enter episode name...', 'menu_icon' => plugins_url('radiofrei/images/rf-menu-icon.png'), // labels 'labels' => array( 'name' => 'Episodes', 'singular_name' => 'Episode' ), // wordpress features 'supports' => array( 'title', 'editor', 'comments', 'revisions' ), // columns in admin 'edit_columns' => array( 'title' => 'Episode' ), // frontend - backend 'public' => true, 'has_archive' => true, // slug 'rewrite' => array( 'slug' => 'episodes' ) ) )); return $post_types; }2. Metabox for rf_episode
<?php /* Title: Related Shows Post Type: rf_episode */ piklist('field', array( 'type' => 'select', 'template' => 'field', 'choices' => piklist(get_posts(array( 'post_type' => 'rf_show', 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' )), array('ID', 'post_title')), 'relate' => array( 'scope' => 'post' ), 'attributes'=> array( 'multiple' => 'multiple' ) ));I see all my shows and can select one/multiple but after saving no selection(s) and no database entries.
-
March 18, 2016 at 2:21 pm #6120
SteveKeymasterTry this it works for me:
piklist('field', array( 'type' => 'select' ,'field' => 'my_relate' ,'template' => 'field' ,'label' => 'Relate Posts' ,'choices' => piklist( get_posts(array( 'post_type' => 'rf_show' ,'numberposts' => -1 ,'orderby' => 'title' ,'order' => 'ASC' )) ,array('ID', 'post_title') ) ,'relate' => array( 'scope' => 'post' ) ,'attributes'=> array( 'multiple' => 'multiple' ) ));This will save relate data in the post_meta table
-
March 18, 2016 at 3:48 pm #6126
stemuedendronMemberYes, this works, it’s the same like the first field in my first post where I wrote that this works. It works bidirectional in the sense that I get two entries in the postmeta table:
– one that links to the selected post ID of post type ‘rf_show’:
post_id: 528
meta_key: my_relate
meta_value: 38– and the second that links back (comes from the ‘relate’ parameter):
post_id: 38
meta_key: __relate_post
meta_value: 528Maybe I’m not precise in my question. My point is that using the same code for a post-to-user relationship I get only one entry in the database. The ‘relate’ parameter does nothing in this case (look my second field in the first post). I get no back-link entry in the database.
Jason said, that I don’t need the back-link, which is true, but it’s confusing that the post-to-post and post-to-user relationship differs on this point. Following this approach (we need only one meta entry) the ‘relate’ parameter is unnecessary.
Then Jason said we should omit the ‘field’ parameter if we use the ‘relate’ parameter, but this
does not work.So I think I use the following ‘classical’ approach without the ‘relate’ parameter but with a ‘field’ parameter:
piklist('field', array( 'type' => 'select' ,'field' => 'my_relate' ,'template' => 'field' ,'label' => 'Relate Posts' ,'choices' => piklist( get_posts(array( 'post_type' => 'rf_show' ,'numberposts' => -1 ,'orderby' => 'title' ,'order' => 'ASC' )) ,array('ID', 'post_title') ) ,'attributes'=> array( 'multiple' => 'multiple' ) ));Regards
-
March 21, 2016 at 12:09 pm #6132
JasonKeymasterHi!
My apologies, I should have talked to Steve before he posted and better explained my intention.
I maintain that no, you don’t need to have a
'field'parameter for relates. In fact, I’m very surprised that Steve’s example worked.Please clear this up for me, though, when you say there are no database entries, do you mean there’s no
__relate_postcreate for the rf_show meta? Have you tried just doing a get_posts query with suppress_filters set to false and using post_has/post_belongs? -
March 21, 2016 at 2:51 pm #6133
stemuedendronMemberHi Jason,
yes, there is no
__relate_postentry in the database and I’m looking directly in the mysql database. I get the__relate_postentry only as long as I give a ‘field’ parameter. -
March 23, 2016 at 10:14 am #6153
SteveKeymaster@stemuedendron– I just emailed you a new version of Piklist. Please test and let us know if it solves your issues.
-
March 23, 2016 at 11:24 am #6157
stemuedendronMemberOk, it’s closer but not finnished.
I have these data:
——————
CPT: rf_shows
name Sendung1
id: 20CPT: rf_episode
name: Folge1
id: 38User
name: Hans
id: 2With this code:
<?php /* Title: Sendeplanung Post Type: rf_episode */ piklist('field', array( 'type' => 'checkbox', 'label' => 'Shows', 'relate' => array( 'scope' => 'post' ), 'choices' => piklist(get_posts(array( 'post_type' => 'rf_show', 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' )), array('ID', 'post_title')) )); piklist('field', array( 'type' => 'checkbox', 'label' => 'DJs', 'relate' => array( 'scope' => 'user' ), 'choices' => piklist(get_users(array( 'orderby' => 'display_name', 'order' => 'ASC' )), array('ID', 'display_name')) ));I get in wp_postmeta:
post_id: 20 meta_key: __relate_post meta_value: 38and in wp_usermeta:
user_id: 2 meta_key: __relate_post meta_value: 38Thats OK. But I did not see the user checked in post editor although the database entry is correct (look at the attachment).
Is my use of post-to-user relationship wrong?
Attachments:
You must be logged in to view attached files.
-
-
AuthorPosts
- You must be logged in to reply to this topic.