Quantcast
Channel: Toolset » All Posts
Viewing all articles
Browse latest Browse all 20145

Reply To: [Waiting for user confirmation] Searching for Posts (fields) that Start With

$
0
0

That was a great poiner. It led me to perfect solution. Just had to add an INNER JOIN clause.

Here's my final code:

<ul class="posts">

        <?php 
         global $wpdb; 
         $request = $_REQUEST["ln"]; // could be any letter you want
         if($request!='')
         {
           $results = $wpdb->get_results(
                  "
                  SELECT * FROM $wpdb->posts
                  INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 
                  WHERE meta_value LIKE '$request%'
                  AND post_type = 'families'
                  AND post_status = 'publish'
                  AND meta_key = 'wpcf-last_name'; 
                  "
           ); 
           if ( $results ) 
           {
              foreach ( $results as $post ) 
              {
                  setup_postdata ( $post ); 
                  ?> 
                  <li>
                      <?php echo '<a href=' . get_permalink($ID) . ' ' . 'title=' . get_the_title($ID) . '>' . get_the_title($ID) . '</a> ';?>
                  </li>
                  <?php 
              }
           } 
           else 
           {
              ?> 
              <div class="alert">No families found matching that criteria. Please try again, or use the search at the top.</div>
              <?php
           }
         }
         ?>
    </ul>
 

Searching by fields "that start with" is possible!

Hope this helps someone!


Viewing all articles
Browse latest Browse all 20145

Trending Articles