1
votes

I'm a senior IT, not a coder. I'm creating a WordPress search.php page into my child theme. This particular page must return results from 3 cpt

  • 'struttura'
  • 'tour'
  • 'offerta'

The below code, returns an array of ID to populate a beautiful grid plugin engine that is called "essential grid" with the search results, the code is working but I have the following issue.

  • If I search 'philippos' (that is a struttura cpt) is returning correct
  • If I search 'senior' (that is a offerta cpt) is returning correct
  • If I search 'alessandro' (that is a tour cpt) is returning correct
  • If I search 'notpresent' that is not present in any cpt is returning the correct echo string that says to make another search
  • If I search 'cargo' that is present in the 'post_type->'page' I'm expecting the same echo string as above, instead it returns some random(???) cpt posts.

Is like searching into page post_type in a wrong way. Any suggestion?

This is the search page http://neos.anekitalia.com/?s

<?php
/*
Template Name: Search Page
*/
?>
<?php
get_header();
?>
<?php echo do_shortcode('[et_pb_section global_module="216661"][/et_pb_section]'); //show section Vuoi fare un'altra ricerca? ?>
<div class="et_pb_section et_section_regular" style="padding: 0;min-height:250px;">
                        <div class="et_pb_row">
                            <div class="et_pb_column et_pb_column_4_4 et_pb_column_14     et_pb_css_mix_blend_mode_passthrough et-last-child" >
                                <div class="et_pb_module et_pb_code">
                                    <div class="et_pb_code_inner">
<?php // Build Search Query
$args = array(
    's'         => $_REQUEST['s'],
    'showposts' => -1,
    'post_type' => array ( 'tour', 'struttura', 'offerta' ) // define searchable Post Types
);
$tp_allsearch = new WP_Query($args);


$posts = array();
// If there are Search posts
if($tp_allsearch->have_posts()) :

// Go through result
while($tp_allsearch->have_posts()) : $tp_allsearch->the_post();

// Save Post ID in array    
$posts[] = $post->ID;

endwhile;
// Build shortcode with the $post array build before to populate essential grid engine
$the_content = do_shortcode('[ess_grid alias="searchdefault" posts="'.implode(',', $posts).'"]');
// Echo Out the result in your page
echo $the_content;

else:
//show section no results found
   echo do_shortcode('<div style="max-width: 800px; margin: 0 auto"> [et_pb_section global_module="216673"][/et_pb_section]</div>');
endif;
?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
<!--footer and stuffs-->                    
                    <?php echo do_shortcode('[et_pb_section global_module="208275"][/et_pb_section]');?>
                        <?php echo do_shortcode('[et_pb_section global_module="210037"][/et_pb_section]');?>
                    <?php echo do_shortcode('[et_pb_section global_module="210036"][/et_pb_section]');?>
<?php

get_footer();

EDIT:

I found why this is happening, simply the all the text that I was searching were present into a div section on the bottom so I have to find a way to exclude text present into the content, is there a way to make the query only into the title and exclude the content?

1
sorry but I want to fix this by myself - silvered.dragon
I found why this is happening, simply the all the text that I was searching were present into a div section on the bottom so I have to find a way to exclude text present into the content, is there a way to make the query only into the title and exclude the content? - silvered.dragon

1 Answers

0
votes

I found this in class-wp-query

$search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );

By default $search will look something like:

    (wp_posts.post_title LIKE %s) 
    OR (wp_posts.post_excerpt LIKE %s) 
    OR (wp_posts.post_content LIKE %s)
    AND (wp_posts.post_password = '')  

So it looks like you could add a filter to your theme's functions.php. Its quite tricky to remove the post content and post excerpt parts of the query with preg replace or whatever but you could string manipulate it so that its more like:

    (wp_posts.post_title LIKE %s) 
    OR (wp_posts.post_title LIKE %s) 
    OR (wp_posts.post_title LIKE %s)
    AND (wp_posts.post_password = '') 

by doing something like this

add_filter('posts_search', 'so_54550119_search_filter');
function so_54550119_search_filter($search){
   return str_replace(array('excerpt','content'), array('title','title'), $search);
}