April 5, 2017 at 12:51 am #7902
Hi, I have a fairly complicated site built atop Piklist. I have been storing repeating groups of (add more) data (in the postmeta table) along with the custom post types I have built. And I need a more reliable way than index to get at these groups for editing and updating. I would have liked to have used grouped fields (serialized in the db), but because my groupings have many conditional values, this won’t currently work in Piklist (see this link).
So here are my questions to solve this:
1. Could I store these groups in their own post type and relate them to the parent CPT?
2. If so, is there a way to allow these child posts to both display AND be editable from within the parent in admin?
Thanks for any advice or help!
April 5, 2017 at 1:04 am #7903DanParticipant
April 5, 2017 at 1:54 am #7904
I found something in the demo that gets at this idea: /parts/meta-boxes/field-relationship.php
BUT, I can’t figure out how to add post-meta data to the related post. It will save the title and content shown in the demo, but every time I try to add another field there (for meta-data of the related post) it fails to save into the related post. Is this even possible?
April 5, 2017 at 1:23 pm #7907
Has anyone done something like this? Does anyone have any example code?
April 5, 2017 at 10:09 pm #7913friendlyfire3Member
I see what you’re trying to do and have tried something similar myself (editing a related post meta in the parent) but wasn’t able to do so in any clear or useful way only via piklist.
I’m also having issues with the conditionals using addmores and nested groups. I’ve just decided to wait until the feature is released (hopefully soon.)
April 6, 2017 at 2:04 am #7915
Here is what I have tried/noticed so far. The following code is only a slight change from the demo, but it works/fails in the following ways:
- I can relate posts from my CPT “action”, and they will show with the edit box and meta data field.
- I can update the regular POST fields like Title and content
- I CAN NOT update the associated meta data field (expiration_date). When changed it reverts back to whatever the separate CPT holds.
- Changing expiration_date to have a scope of post_meta links it to the parent instead of the child.
- Changing scope to the name of my CPT (‘action’) results in nothing working, just blank fields not attached to anything.
- I also notice that clicking “save” while in the parent will unfortunately publish instead (I suspect due to the presence on the child post of hidden field with post status of publish, but clearly it should only update the child status, not the parent)
piklist('field', array( 'type' => 'checkbox' ,'field' => '_' . piklist::$prefix . 'relate_post' ,'label' => __('Relate Posts', 'my-domain') ,'choices' => piklist( get_posts(array( 'post_type' => 'action' ,'numberposts' => -1 ,'orderby' => 'title' ,'order' => 'ASC' )) ,array('ID', 'post_title') ) ,'relate' => array( 'scope' => 'post' ) )); $related = get_posts(array( 'post_type' => 'action' ,'posts_per_page' => -1 ,'post_belongs' => $post->ID ,'post_status' => 'publish' ,'suppress_filters' => false )); piklist('field', array( 'type' => 'group' ,'scope' => 'post' ,'label' => 'Related Posts' ,'relate' => array( 'scope' => 'post' ) ,'add_more'=> true ,'fields' => array( array( 'type' => 'datepicker' ,'field' => 'expiration_date' ,'required' => true ,'label' => __('Expiration Date', 'my-domain') ,'help' => __('Choose an expiration date for this item.', 'my-domain') ,'options' => array( 'dateFormat' => 'yy-mm-dd' ) ,'attributes' => array( 'size' => 17 ) ) ,array( 'type' => 'text' ,'field' => 'post_title' ,'label' => 'Post Title' ,'columns' => 12 ) ,array( 'type' => 'textarea' ,'field' => 'post_content' ,'label' => 'Post Content' ,'columns' => 12 ,'attributes' => array( 'rows' => 5 ) ) ,array( 'type' => 'hidden' ,'field' => 'post_status' ,'value' => 'publish' ) ) ));
April 21, 2017 at 12:10 am #7949
Question for Steve: does the code currently up at github fix these problems? I have been combing through it and see reference in a comment to “Handle relate groups”
April 21, 2017 at 11:01 am #7952
April 21, 2017 at 11:08 am #7954
@sbruner – thanks, I will check it out when I get a chance. For the problem above, I ended up having to custom code my own backend ajax related post system, the only parts of piklist in use for that are the postmeta “__relate_post” meta key and the query parameter “post_belongs”, but that should allow me (when all of that is working) to hopefully swap it out. I will let you know what I find.
April 21, 2017 at 12:06 pm #7956JasonKeymaster
I use relates quite regularly and should work perfectly in the latest RC version. Please grab it from Github and, if you have any issues and are comfortable with Github, please make an issue with any details you can provide. Better yet, as we’re trying to make Piklist core more about community contribution, feel free to make a pull request if you think you’ve found the solution.
April 27, 2017 at 10:45 am #8006
Hi @stephen @steve, Just tried release candidate. It does create a new post however post type is set to revision instead of the post_type passed like so
,array( 'type' => 'hidden', 'field' => 'post_type', 'value' => 'my-custom-type', ) ,array( 'type' => 'hidden' ,'field' => 'post_status' ,'value' => 'publish' )
Am I passing the parameter as expected or am I missing something ?
April 27, 2017 at 10:08 pm #8017
April 28, 2017 at 2:22 am #8018
@Steve yes, it is registered and I can see it in WordPress Menu.
To make it more concrete, sharing CPT registration with screenshot (attached)
// Setup Payment Plan $post_types['payment-plan'] = array( 'labels' => piklist('post_type_labels', 'Payment Plan') ,'title' => __('Enter a new Payment Plan Name') ,'public' => true ,'rewrite' => array('slug' => 'payment-plan') ,'show_in_rest' => true ,'supports' => array( 'title' ,'revisions' ) ,'hide_meta_box' => array( 'revisions' ,'comments' ,'commentstatus' ) ,'menu_icon' => 'dashicons-dashboard' ,'menu_position' => 10 ,'capability_type' => array('payment-plan', 'payment-plans') ,'map_meta_cap' => true ); return $post_types;
This is code to add related post meta box
piklist('field', array( 'type' => 'group' ,'scope' => 'post' ,'label' => 'Related Posts' ,'relate' => array( 'scope' => 'post' ) ,'add_more'=> true ,'fields' => array( array( 'type' => 'text' ,'field' => 'post_title' ,'label' => 'Amount Due' ,'columns' => 12 ) ,array( 'type' => 'text' ,'field' => 'post_content' ,'label' => 'Post Content' ,'columns' => 12 ,'attributes' => array( 'rows' => 5 ) ) ,array( 'type' => 'hidden', 'field' => 'post_type', 'value' => 'payment-plan', // 'scope' => 'post' ) ,array( 'type' => 'hidden' ,'field' => 'post_status' ,'value' => 'publish' ) ) ));
Attachments:You must be logged in to view attached files.
April 28, 2017 at 2:51 am #8020
For testing I have used title and content, I have also tried with custom fields for that post type. I am sure the content will not be saved as I have not added it in supports.
1. Is the post_type being passed correctly ?
2. Can we populate other fields registered as custom metabox for related CPT data entry via relations.
- You must be logged in to reply to this topic.