0

I have the following code which display different things, that's why I'm using many filters in the query. The problem is that it displays the same thing, no matter the query from the switch.

If I do something like

SELECT * FROM wp_posts WHERE ID='100'

as example, it will display

SELECT * FROM wp_posts ... 

Also, I've added where post_type='post' and it display all kind of things that he found in the database, like pictures, contact form, etc.

 if(isset($_GET["submit"])){

  $nume_searchq=$_GET["nume_doc"];
  $spec_searchq=$_GET["specializare_doc"];
  $instit_searchq=$_GET["spital_doc"];

 switch($_GET['filter1']){


  case "ALL":
    $querystr = "SELECT * FROM `wp_posts` WHERE post_type='post' OR LOWER(post_title) LIKE '%$nume_searchq%' OR LOWER(post_content) LIKE '%$spec_searchq%' OR LOWER(post_content) LIKE '%$instit_searchq%' LIMIT $limit";
    break;
case "AB":
    $querystr = "SELECT * FROM `wp_posts` WHERE post_content LIKE '%Alba%' AND post_type='post' OR LOWER(post_title) LIKE '%$nume_searchq%' OR LOWER(post_content) LIKE '%$spec_searchq%' OR LOWER(post_content) LIKE '%$instit_searchq%' LIMIT $limit ";
    break;
case "AG":
    $querystr = "SELECT * FROM `wp_posts` WHERE post_content LIKE '%Arges%' AND post_type='post' OR LOWER(post_title) LIKE '%$nume_searchq%' OR LOWER(post_content) LIKE '%$spec_searchq%' OR LOWER(post_content) LIKE '%$instit_searchq%' LIMIT $limit ";
    }

  $pageposts = $wpdb->get_results($querystr, OBJECT);?>
  <?php if ($pageposts): ?>
  <?php foreach ($pageposts as $post): ?>
   <?php setup_postdata($post); ?>

  <div class="post" id="post-<?php the_ID(); ?>">
  <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
     <?php the_title(); ?></a></h2>
    <?php //display_rating_result(); ?>
    <div class="entry">
   <?php the_content('Read the rest of this entry »'); ?>
    </div>
    <?php edit_post_link('Edit', '', ' | '); ?>  
   <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
  </div>
  <hr>
  <?php endforeach; ?>
  <?php else : ?>
    <h2 class="center">Not Found</h2>
  <?php endif; ?>

Where might be the problem in these queries?

1 Answer 1

0

When you do this:

$querystr = "SELECT * FROM `wp_posts` 
WHERE post_content 
    LIKE '%Alba%' 
    AND post_type='post' 
OR 
    LOWER(post_title) 
    LIKE '%$nume_searchq%' 
OR 
    LOWER(post_content) 
    LIKE '%$spec_searchq%' 
OR 
    LOWER(post_content) 
    LIKE '%$instit_searchq%' 
LIMIT $limit ";

If ANY of the variables are blank ($nume_searchq || $spec_searchq || $instit_searchq ), you will get all posts, because LIKE '%%' matches everything.

So this leaves you with a couple of options.

1) Explicitly set each of those variables every time --or--

2) Conditionally include them depending on if they contain something valuable.

For example:

case "AB":
    $querystr = "SELECT * FROM `wp_posts` WHERE post_content LIKE '%Alba%' AND post_type='post'";

    if ( isset( $nume_searchq ) && $nume_searchq !== '' )
        $querystr .= " OR LOWER(post_title) LIKE '%$nume_searchq%'";

    if ( isset( $spec_searchq ) && $spec_searchq !== '' )
        $querystr .= " OR LOWER(post_content) LIKE '%$spec_searchq%'";

    if ( isset( $instit_searchq ) && $instit_searchq !== '' )
        $querystr .= " OR LOWER(post_content) LIKE '%$instit_searchq%'";

    $querystr .= " LIMIT $limit ";

    break;
1
  • Wow dude, thanks alot:) its perfect, you saved me.
    – Frapping
    Commented Aug 7, 2014 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.