How to query search only for post titles?

admin2025-06-06  6

I tried this but I keep getting all posts

           $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
            );

I even tried

'title' => $s

But it is not limiting it to title.

Here it is a bit more of the bit of code:

First I check the search is getting a value form a form checkbox title and if so I run the query to only search within the titles

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

I tried this but I keep getting all posts

           $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
            );

I even tried

'title' => $s

But it is not limiting it to title.

Here it is a bit more of the bit of code:

First I check the search is getting a value form a form checkbox title and if so I run the query to only search within the titles

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 } 
                    } 
Share Improve this question edited Nov 13, 2018 at 10:41 rob.m asked Nov 13, 2018 at 10:17 rob.mrob.m 2072 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try this

    $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; 
?>

Please let me know if any query.

Hope it will help you!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749185909a317081.html

最新回复(0)