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?