February 20, 2014 at 4:32 pm #1431
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.
February 21, 2014 at 4:02 am #1435
Ahh, 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.
February 21, 2014 at 4:23 pm #1438
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
Also, 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
Nevermind 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
I’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
Hey 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.
February 22, 2014 at 1:48 pm #1445
Hey 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
@Jason– The current post-relate field is going to go away and will be replaced with a more configurable field, before v1.0. It’s just going to take some time to flush out the details.
February 24, 2014 at 10:25 am #1449
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
I 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
February 25, 2014 at 1:35 pm #1460
@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':
Just make sure you update this every time you update Piklist, until we get this flushed out.
February 25, 2014 at 4:56 pm #1462
I’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
@Jason– I was referring to your original post: “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?”
July 20, 2016 at 1:26 pm #6983
How 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
I 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
Can you please post your field here so I can take a look?
July 25, 2016 at 5:43 pm #7020
I’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:
July 25, 2016 at 6:50 pm #7021
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' ) ));
Hope this helps! 🙂
July 25, 2016 at 11:53 pm #7023
That only displays the default option, not the suppliers also 🙁
July 26, 2016 at 12:28 am #7024
I’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
That’s what’s happening, using the latest version.
July 27, 2016 at 4:31 pm #7035
If 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.
You must be logged in to reply to this topic.