Viewing 24 reply threads
  • Author
    Posts
    • #1431
      Jason
      Keymaster

      Greetings!

      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!

    • #1435
      Marcus
      Member

      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.

      Marcus

    • #1438
      Jason
      Keymaster

      Hey 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

    • #1439
      Jason
      Keymaster

      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.

    • #1440
      Jason
      Keymaster

      Nevermind about the line. I figured it out. But I’m still having difficulties getting this to work. 🙁

    • #1441
      Jason
      Keymaster

      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.

    • #1444
      Marcus
      Member

      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.

      Marcus

    • #1445
      Jason
      Keymaster

      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.

      Gist: https://gist.github.com/JasonTheAdams/9142065

    • #1448
      Steve
      Keymaster

      @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.

    • #1449
      Jason
      Keymaster

      @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.

    • #1450
      Jason
      Keymaster

      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.

    • #1455
      Jason
      Keymaster

      I 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.

    • #1460
      Steve
      Keymaster

      @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.php

      Just make sure you update this every time you update Piklist, until we get this flushed out.

    • #1462
      Jason
      Keymaster

      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.

    • #1464
      Steve
      Keymaster

      @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?”

    • #6983
      catacaly
      Member

      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.

    • #6984
      Jason
      Keymaster

      Hi @catacaly!

      You wouldn’t change the scope to a custom post type, as the object is a post (therefore the scope is post). If you want to relate to a custom post type, you just limit the choices to that post type.

      Hope this helps! 🙂

    • #6986
      catacaly
      Member

      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?

    • #6993
      Jason
      Keymaster

      Can you please post your field here so I can take a look?

    • #7020
      catacaly
      Member

      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:

      <?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>
    • #7021
      Jason
      Keymaster

      Hi @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! 🙂

    • #7023
      catacaly
      Member

      That only displays the default option, not the suppliers also 🙁

    • #7024
      Jason
      Keymaster

      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.

    • #7029
      catacaly
      Member

      That’s what’s happening, using the latest version.

    • #7035
      Jason
      Keymaster

      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.

Viewing 24 reply threads
  • You must be logged in to reply to this topic.