• Resolved johnrage

    (@johnrage)


    Currently creating a custom search form where wordpress members able to search for registered users based in their professions.

    Below is my code:

    <form method=”get” id=”searchform” action=”<?php echo esc_url( home_url( ‘/’ ) ); ?>”>
    <label for=”s” class=”assistive-text”><?php _e( ‘Search’, ‘twentyeleven’ ); ?></label>

    <select name=”s” id=”s” class=”field”>
    <option value=”” selected=”selected”>– Search a Pros — </option>
    <option value=”s: doctor”>Doctor</option>
    <option value=”s: psychologist”>Psychologist</option>
    <option value=”s: Dentist”>Dentist</option>
    </select>
    <input type=”submit” class=”submit” name=”submit” id=”searchsubmit” value=”<?php esc_attr_e( ‘Search’, ‘twentyeleven’ ); ?>” />
    </form>

    When I search I get no results which I believe there is something here that I am missing. Some guys told me a query to mysql table will do the work. Can you point me what to do here?

    Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi,
    The search form can be considered correct, the way you make the search page is much more important, you can start learning about search pages here.
    Regards

    Thread Starter johnrage

    (@johnrage)

    Thanks for the quick response appreciated. Can you point me other detailed references how to work around with the search page that address to my questions above?

    Thread Starter johnrage

    (@johnrage)

    Now I have my query below from my searchform.php and I get a blank result.

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]

    global $wpdb;
    
    $s=(isset($_GET['s']));
    
    $query = mysql_query('SELECT * FROM usermeta WHERE $wpdb->description = $s');
    
    while($row = mysql_fetch_array($query)){
          echo $row[0] . "";
    }

    I see you don’t use wordpress functions to achieve the results of your search, you’ll have to learn some of the wordpress functions in order to create good code. Hope I won’t be understood wrong, you can achieve this with php and mysql only also but this is not the best practice withing wordpress;
    Start learning about query_posts here:
    https://codex.www.ads-software.com/Function_Reference/query_posts
    and get_posts here:
    https://codex.www.ads-software.com/Template_Tags/get_posts
    And you can also read about the class wp_query (this can be too much for the start but the first 2 are accessible ):
    https://codex.www.ads-software.com/Class_Reference/WP_Query

    Thread Starter johnrage

    (@johnrage)

    Thanks man, partially I got it working the search query. It retrieve now to the db and display results however I need to refine it further. The query displays all the names of the “doctor”. How could I extract the input string so that specific names only will appear in the results.

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. It makes the code much easier to read and follow. ]

    <?php
    	global $wpdb;
    	$metavalue = $_GET['s'];
    	$selectvalue = $_GET['select_pros'];
    
    	if ($selectvalue == 'doctor') {
    
    		//Query All Description with Doctor
    		$att_qry = "SELECT *
    			FROM $wpdb->usermeta
    			WHERE meta_key='description'
    			AND meta_value='doctor'
    			LIKE meta_value='$metavalue%'
    			";
    
    		$att_qry_results = $wpdb->get_results($att_qry);
    		  foreach ($att_qry_results as $att_qry_result) {
    
    			//Query User Information
    			$userinfo=get_userdata( $att_qry_result->user_id );
    
    			//Display Results
    			echo '
    <li>' . $userinfo->first_name . ' ' . $userinfo->last_name . '</li>
    ';
    
    			}
    	}
    Thread Starter johnrage

    (@johnrage)

    Working now:

    $query = “SELECT user_id FROM $wpdb->usermeta
    WHERE (meta_value LIKE ‘%%$metavalue%%’)
    AND (meta_key = ‘first_name’)
    AND user_id IN (SELECT user_id FROM $wpdb->usermeta WHERE (meta_value= ‘doctor’) AND (meta_key = ‘description’))”;

    I found the post in this URL:https://wordpress.stackexchange.com/questions/28589/search-users-base-on-meta-value-meta-key

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to pass the select value?’ is closed to new replies.