我试过了,但我一直收到所有的帖子
$post_ids_post = $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT ID FROM {$wpdb->posts}
WHERE post_title LIKE \'%s\'
", $search, $search ) );
$tit = array(
\'post__in\' => $post_ids_post,
\'s\' => $s
);
我甚至试过
\'title\' => $s
但这并不局限于标题。
这里有更多的代码:
首先,我检查搜索是否从表单复选框标题中获取值,如果是,我运行查询以仅在标题中搜索
elseif($sel === "title") {
$tit = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."posts WHERE post_title LIKE \'%" . $_REQUEST[\'s\'] ."%\' AND post_status = \'publish\' AND post_type = \'post\'");
if ( count($tit) > 0 ){ ?>
<li class="animated zoomIn">
<div class="tldate"><?php echo $year; ?></div>
</li>
<li class="post postCount<?php echo $thenumber; ?>">
<div class="tl-circ"></div>
<div class="tl-text"><?php echo $giorno; ?>/<?php echo $mese; ?></div>
<div class="timeline-panel wow fadeInLeft">
<div class="vis_del">
<?php
$img = usp_get_meta(false, \'usp-file-single\');
$pathtofile = $img;
$info = pathinfo($pathtofile);
if ($info["extension"] == "jpg") { ?>
<img src="<?php echo $img; ?>" class="bnr_img img-responsive center-block" alt="">
<?php } else { ?>
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="<?php echo $img; ?>"></iframe>
</div>
<?php } ?>
<h4 class="font-bold"><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?> " class="btn_green">leggi tutto</a>
<div class="vis_soc_wrap">
<ul class="list-inline text-right social_list">
<li>
<a href="<?php echo get_home_url(); ?>/create/" data-toggle="tooltip" title="aggiungi ricordo">
<i class="fas fa-plus-circle c_icon"></i>
</a>
</li>
</ul>
</div>
</div>
</div>
</li>
<?php }
}
最合适的回答,由SO网友:Pratik Patel 整理而成
试试这个
$posts = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."posts WHERE post_title LIKE \'%" . $_REQUEST[\'s\'] ."%\' AND post_status = \'publish\' AND post_type = \'post\'");
$do_not_duplicate = $post->ID;
foreach($tit as $post) {
if( $post->ID == $do_not_duplicate ) continue;
?>
如果有任何疑问,请告诉我。
希望它能帮助你!