Tagged: metabox, post-relate, select
- This topic has 24 replies, 4 voices, and was last updated 5 years, 6 months ago by
Jason.
-
AuthorPosts
-
-
February 20, 2014 at 4:32 pm #1431
JasonKeymasterGreetings!
The post-relate currently uses a checklist format for selecting multiple posts. I’d like to make a Select so only one can be chosen. What’s the best way to go about doing this?
It doesn’t seem dynamic at the moment, should I insert the values into the wp_post_relationships table myself? I’m currently pulling all the posts into the select, but this, of course, doesn’t support the post_belongs and post_has arguments. I’d just have to figure out how to add an action that does this when submitted.
Thanks!
-
February 21, 2014 at 4:02 am #1435
MarcusMemberAhh, youre starting to get under the hood Jason.
This is a great thing.
Take a look at the parts/fields folder.
Take the post-relate.php and the select.php and see if you can’t make a new field called post-relate-select.php and make your own brand new field type.🙂
Once you’ve got field creation down, you’ll be amazed at the things you can do with piklist.
Marcus
-
February 21, 2014 at 4:23 pm #1438
JasonKeymasterHey Marcus!
Thanks for pointing me in the right direction!
I’m working on a custom field, and while I’m getting the select to populate with the items, I’m having trouble getting the relationship to save.
What’s confusing me is how the post-relate field works. There’s two hidden fields, $scope_relate and $scope_relate_remove, and I assume the values applied to these fields determines whether an id is added or removed. The only values applied, however, are to $scope_relate_remove; no values are ever added to $scope_relate, although it has a ‘value’ => ‘has’ field. I’m not sure what that does. Does it simply determine the relationship to be ‘has’ or ‘belongs’?
My code can be found here: https://gist.github.com/JasonTheAdams/9142065
-
February 21, 2014 at 5:12 pm #1439
JasonKeymasterAlso, please help me understand this line:
$(‘:input.<?php echo $scope . ‘_post_id’; ?>[value=”‘ + current_value + ‘”]’).prop(‘checked’, $(this).is(‘:checked’));I’m having trouble understanding what this is accomplishing.
-
February 21, 2014 at 5:48 pm #1440
JasonKeymasterNevermind about the line. I figured it out. But I’m still having difficulties getting this to work. 🙁
-
February 21, 2014 at 6:03 pm #1441
JasonKeymasterI’m close! The record is being added to the database… but it’s adding a row with has_post_id = 0. I assume it’s adding one per post, but I’m only experimenting with two.
-
February 22, 2014 at 10:38 am #1444
MarcusMemberHey Jason, I was going to comment, but I can see from your gist revisions that you fixed most of your problems.
I haven’t had a chance to copy your gist and try it, but the post_id=0 do you think it may have anything to do with your default setting? I can’t see another issue, but I haven’t installed it yet. Will post again when I have.
Marcus
-
February 22, 2014 at 1:48 pm #1445
JasonKeymasterHey Marcus! Well.. I’ve dabbled a bit more, and while I think it’s functioning a bit more robustly, I can’t seem to figure out the extraneous post_id=0 bug. I’m really running out of ideas. There’s clearly something going on that I can’t see, as I’m never even appending values to the $scope_select_relate field; only the remove field. I’m wondering if the magic behind that is having an issue.
When you say “default setting”, are you referring to the $default variable?
Thanks! Hoping to give this one to the Piklist team when all is said and done so it can lunch as another ready-made field.
-
February 23, 2014 at 11:11 pm #1448
-
February 24, 2014 at 10:25 am #1449
JasonKeymaster@Steve
That sounds great! ..But I kind of need this now. Can you please offer any insight into my situation? I’m starting to spend too much time on this particular asset for the project I’m working on. -
February 24, 2014 at 12:16 pm #1450
JasonKeymasterI can’t spend any more time on this, presently. But I’m still very open to suggestions.
In case someone else needs this before the post-relate is configurable, my workaround is going to be to use the post__not_in = array(0) argument to sift out the bad references. This does, unfortunately, mean unnecessary rows in the database, but that’s not a big deal.
-
February 24, 2014 at 6:14 pm #1455
JasonKeymasterI have the gist (https://gist.github.com/JasonTheAdams/9142065) to a point where it’s storing the relationship. The only bug is that if I use this simultaneously to the post-relate field it stores the extraneous field where the post_id is correct, but the has_post_id is 0.
-
February 25, 2014 at 1:35 pm #1460
SteveKeymaster@Jason– Though we don’t love recommending this, your best option right now is to overwrite the post relate field, and change
'type' => 'checkbox'to'type' => 'select':parts/fields/post-relate.phpJust make sure you update this every time you update Piklist, until we get this flushed out.
-
February 25, 2014 at 4:56 pm #1462
JasonKeymasterI’m a bit confused as to to what that would solve? As specified in the other thread, I’m using both post-relate and my own select-relate. Wouldn’t changing that line break post-relate as it is? I need both to work side-by-side.
For a bit I’m going to move on from this. Adding the hidden field to select-relate did enough for me to differentiate the fields, and I’ll include post__not_in (array(0)) in my queries to ignore the has_post_id = 0 values. It’s not awesome, but it works.
-
February 25, 2014 at 5:28 pm #1464
-
July 20, 2016 at 1:26 pm #6983
catacalyMemberHow can I change this to a custom post type as the scope instead? I tried setting $scope = “‘my_cpt'”; didn’t work, options go blank.
-
July 20, 2016 at 1:34 pm #6984
-
July 20, 2016 at 6:37 pm #6986
catacalyMemberI changed the value for post_type < get_posts < choices < field, still yields blank. What else do I need to change? Also how do I add a null default option?
-
July 21, 2016 at 7:35 pm #6993
JasonKeymasterCan you please post your field here so I can take a look?
-
July 25, 2016 at 5:43 pm #7020
catacalyMemberI’m groggy right now but this is what I have so far for meta-boxes/product.php, which I threw together from some code found on these forums mixed with my own inferred:
<?php /* Title: Supplier Post Type: product Context: side */ piklist('field', array( 'type' => 'post-relate' ,'field' => 'Supplier' ,'scope' => 'product_supplier' ,'field' => 'prod_supplier' )); /* $relate = get_posts(array( 'post_type' => 'product_supplier' // Set post type you are relating to. ,'posts_per_page' => -1 // ,'post_belongs' => $post->ID ,'post_status' => 'publish' ,'suppress_filters' => false // This must be set to false )); foreach ($relate as $related): echo $related->post_title; endforeach; */ //$scope = "'product_supplier'"; $posts = get_posts(array( 'post_type' => $scope, 'post_belongs' => $post->ID, 'post_per_page' => -1, 'suppress_filters'=> false )); $default = get_post_meta( $post->ID, $field, true ); $default = empty($default) ? null : $default; piklist('field', array( 'type' => 'select', 'scope' => 'relate', 'field' => $scope . '_select_post_id', 'template' => 'field', 'value' => $default, 'choices' => piklist( get_posts(array( 'post_type' => 'product_supplier',//$scope, 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' )), array('ID', 'post_title') ), 'attributes' => array( 'class' => $scope . '_select_post_id' ) )); piklist('field', array( 'type' => 'hidden', 'field' => $field, 'value' => $default, 'attributes' => array( 'class' => $field . '_select_id' ) )); piklist('field', array( 'type' => 'hidden', 'scope' => 'relate', 'field' => $scope . '_select_relate', 'value' => 'has' )); piklist('field', array( 'type' => 'hidden', 'scope' => 'relate', 'field' => $scope . '_select_relate_remove', 'attributes' => array( 'class' => $scope . '_select_relate_remove' ) )); wp_reset_postdata(); ?> <script type="text/javascript"> (function($) { $(document).ready(function() { var $select = $('.<?php echo $scope . '_select_post_id'; ?>'), initialValue = <?php echo ( $default) ? $default : -1 ; ?>; $select.change(function(event) { event.stopPropagation(); var $remove = $('.<?php echo $scope . '_select_relate_remove'; ?>'), $hiddenID = $('.<?php echo $field . '_select_id'; ?>'), currentValue = $(this).val(), ids = []; if ( currentValue != initialValue ) { $(this).children('option').each(function() { var value = $(this).val(); if (value !== currentValue) ids.push($(this).val()); }); } $hiddenID.val(currentValue); $remove.val(ids.join(',')); }); }); })(jQuery); </script> -
July 25, 2016 at 6:50 pm #7021
JasonKeymasterHi @catacaly!
Oh, wow. That’s definitely the product of looking over generations of Piklist code. Making a Piklist relate field is much, much simpler these days.
Check this out:
piklist('field', array( 'type' => 'select', 'Label' => 'Supplier', 'choices' => array('' => 'No Supplier') + piklist(get_posts(array( 'numberposts' => -1, 'post_type' => 'product_supplier' )), array('ID', 'post_title')), 'relate' => array( 'scope' => 'post' ) ));Simple as that. Grab the posts like you normally would using get_posts, then use a handy function Piklist provides to turn the results into an array where the key is the ID and the value is the post_title. You can use this with checkboxes, select fields, radios, etc.. No javascript or anything like that is necessary.
Hope this helps! 🙂
-
July 25, 2016 at 11:53 pm #7023
catacalyMemberThat only displays the default option, not the suppliers also 🙁
-
July 26, 2016 at 12:28 am #7024
JasonKeymasterI’m not sure I follow. So it’s showing the “No Supplier” option, but not the rest of the suppliers? Are you using the latest version of Piklist?
The simple part should be getting all the suppliers to show up. If the get_posts query is correct, then they’ll show up. There must be something else going on.
-
July 26, 2016 at 5:22 pm #7029
catacalyMemberThat’s what’s happening, using the latest version.
-
July 27, 2016 at 4:31 pm #7035
JasonKeymasterIf you use the get_posts with the same parameters elsewhere do you get the posts? That just means something must be wrong with the query itself. Perhaps the post type is misspelled? I’d try doing the query outside of the field and see what you’re getting.
-
-
AuthorPosts
- You must be logged in to reply to this topic.