Custom query posts error

时间:2014-08-07 作者:Frapping

我有以下显示不同内容的代码,这就是为什么我在查询中使用了许多过滤器。问题是,无论来自交换机的查询是什么,它都会显示相同的内容。

如果我这样做

SELECT * FROM wp_posts WHERE ID=\'100\'
例如,它将显示

SELECT * FROM wp_posts ... 
此外,我还添加了post_type=\'post\' 它显示了他在数据库中找到的所有东西,比如图片、联系方式等等。

 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; ?>
这些查询中的问题可能在哪里?

1 个回复
最合适的回答,由SO网友:BA_Webimax 整理而成

执行此操作时:

$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 ";
如果任何变量为空($nume_searchq || $spec_searchq || $instit_searchq ), 您将获得所有帖子,因为LIKE \'%%\' 匹配所有内容。

所以这给你留下了两个选择。

1) 每次显式设置这些变量中的每一个--或者--

2) 根据它们是否包含有价值的内容,有条件地包含它们。

例如:

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;

结束

相关推荐