A
adige72

  • Jun 28, 2011
  • Joined Apr 13, 2011
  • I have an input text and want it to suggest custom taxonomy terms as soon as i type on it.

    These codes below i found after some googling, gives the idea about how to use autocomplete method in WP but i can't figure out how to achieve custom taxonomy terms suggestion in the input box.

    suggest.js:

        $( '#tags' ).autocomplete(
        {
        	minLength: 2,
        	source: function( request, response ) {
        		jQuery.getJSON( MyAjax_object.ajaxurl + "?submittags=?&action=myajax-submit", request, function( data ) {
        			response( jQuery.map( data, function( item ) {
        				jQuery.each( item, function( i, val ) {
        					val.label = val.whatever; // build result for autocomplete from suggestion array data
        				} );
        					return item;
        			} ) );
        		} );  
    }, });

    functions.php:

        function enque_template_scripts() {
        wp_enqueue_script( 'myajax_jsfile_handle', get_bloginfo('template_directory')."/suggest.js", array( 'jquery', 'jquery-form', 'json2' ), false, true ); );
        wp_localize_script( 
            'myajax_jsfile_handle', 
            'MyAjax_object', 
            array( 
                'ajaxurl' => admin_url( 'admin-ajax.php' ),
                'myajax_nonce' => wp_create_nonce( 'myajax_nonce_val' ),
                'action' => 'myajax-submit'
            )
        );
        add_action('get_header', 'enque_template_scripts');
    
    function get_my_suggestions() {
    // This function should query the database and get results as an array of rows:
    // GET the recieved data: 'term' (what has been typed by the user)
    $term = $_GET['term']
    $suggestions_array = array();
    
    // echo JSON to page  and exit.
    $response = $_GET["submittags"]."(". json_encode($suggestions_array) .")";  
    echo $response;  
    exit;
    }
    add_action( 'wp_ajax_myajax-submit', 'get_my_suggestions' );

    input text:

        <input type="text" id="tags" name="submittags" value="" autocomplete="false" />

    I guess the trick would be in:

    [code=html]val.label = val.whatever;[/code]

    and in

    get_my_suggestions

    function. But how?

    Any idea?

    Many thanks in advance.

    • Below code is what i found from a search in web.

      Suppose i have a taxonomy called Color and terms are Blue, Green, Red, Yellow, White. I want when i select Blue, Green and Red and click submit button, i want to get ALL posts those have Blue OR Green OR Red (OR etc.) taxonomy term on the search results page.

      Since the code below is just echoes "Insert Query - Blue, Insert Query - Red ...", is there any way to convert this into a code that in which i can get posts?

      Thanks in advance.

      <?php
      if(isset($_POST['clickhere'])) {
      	$selected_values = $_POST['color'];
      	foreach($selected_values as $key=>$value) {
                      // Insert query comes here.
      		echo "Insert Query - ".$value."<br />";
      	}
      
      $select_blue = (in_array('1',$selected_values)) ? "selected" : "";
      $select_green = (in_array('2',$selected_values)) ? "selected" : "";
      $select_red = (in_array('3',$selected_values)) ? "selected" : "";
      $select_yellow = (in_array('4',$selected_values)) ? "selected" : "";
      $select_white = (in_array('5',$selected_values)) ? "selected" : "";
      }
      ?>
      
      <form name='testform' action='sometest.php' method='post'>
      <select name='color[]' style="border:0px;" size=6 multiple>
      <option value='' selected>Select Item</option>
      <option value='1' <?php echo $select_blue;?> >Blue</option>
      <option value='2'<?php echo $select_green;?> >Green</option>
      <option value='3'<?php echo $select_red;?> >Red</option>
      <option value='4'<?php echo $select_yellow;?> >Yellow</option>
      <option value='5'<?php echo $select_white;?> >White</option>
      </select>
      <br /><br />
      <input type='submit' name='clickhere' value='Click Here' />
      </form>
      • Hi.

        I would like to automatically fill in the textbox labeled "Amount 2" after choosing the value of Amount without refreshing the page. I guess it requires javascript or AJAX but i don't know how to do it. Any help please?

        http://i55.tinypic.com/29crpdz.jpg

        $options_amount = array("0","1","2","3","4","5","6","7","8","9","10+");
        
        $metabox = array();
        
        $metabox[] = array(   "name" => "amount",
        				"label" => "Amount",
                            		"desc" => "Choose the amount.",
                            		"std" => "0",
        				"type" => "select",
                            		"options" => $options_amount);
        
        $metabox[] = array(   "name" => "amount2",
        				"label" => "Amount 2",
                            		"desc" => "No need to enter it.",
                            		"std" => "",
                            		"type" => "text");
        • So is there any other way to print posts for "1 OR 2" condition within this if statement?

          • Hi.

            I want to assign two values (numbers) to a variable and here's the code:

            if (( $no_rooms == '1' ) && ($no_rooms2 == '2')) {
                $no_rooms = '1';
                $no_rooms = '2';
            }

            What i want to do is, printing all posts(wordpress) those have 1 OR 2 rooms. But it prints ONLY the posts those have 2 rooms. I'm stucked so help me please.

            I would be grateful of any help and sorry about my english btw.