如何查询只搜索帖子标题?

时间:2018-11-13 作者:rob.m

我试过了,但我一直收到所有的帖子

           $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 } 
                    } 

1 个回复
最合适的回答,由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; 
?>
如果有任何疑问,请告诉我。

希望它能帮助你!

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post