$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to query search only for post titles?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to query search only for post titles?

matteradmin7PV0评论

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!

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far