WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Search Members (2 posts)

  1. CrimsonMoon
    Member
    Posted 1 year ago #

    Hi!

    I created a member directory page that displays members of the site who have one of two specific roles. I want to create a search bar where users can search through that list for members by name.

    Here is my search form code:

    <form  method="post" action="?go"  id="searchform">
    	      <input  type="text" name="name">
    	      <input  type="submit" name="submit" value="Search">
    	    </form>

    Here is the search code I have so far:

    <?php
      if(isset($_POST['submit'])){
      if(isset($_GET['go'])){
      if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){
      $name=$_POST['name'];
      //-query  the database table
      $results = $wpdb->get_results( "SELECT <code>user_id</code> FROM  <code>wp_usermeta</code> WHERE  <code>meta_value</code> LIKE '%" . $name .  "%'" );
      //-create  while loop and loop through result set
      foreach($results as $result){
      //-display the result of the array
      echo $result->user_id;
      }
      }
      else{
      echo  "<p>Please enter a search query</p>";
      }
      }
      }
    ?>

    I've tried a bunch of variations on this, but so far, all of my searches just go to the default "This is somewhat embarrassing, isn’t it?" page.

    How do I achieve a working search bar?

  2. CrimsonMoon
    Member
    Posted 1 year ago #

    Figured it out! I had to change the action to the page so that the results displayed on the same page and not on the default WordPress search.php. Then I used PHP to do an if statement on the set of users that I already generated.

    <form  method="post" action="<?php the_permalink(); ?>"  id="searchform">
                <input type="text" id="search" name="search" value="" />
                <input type="submit" value="Search" id="searchsubmit" />
    	    </form>
    <?php if( isset( $_REQUEST['search'] ) ) {
      $name = $_POST['search'];
      foreach ($allusers as $auser) {
      if (stristr($auser['first'], $name) || stristr($auser['last'], $name)) { ?>

Topic Closed

This topic has been closed to new replies.

About this Topic