I am looking for a way to perform a search, and while performing the search, it checks a custom field named ‘keywords,’ the post title, and the post content. If any of those fields have results like what the user searches, it will display the custom post type (programs) in the results page.
I do not need help with the end display result (it currently does what I want it to do), but I need to make it so when it searches it checks all three fields (post title, post content, and custom field keywords), and if any of them have a result like what was searched, it displays the results. This is the code I have so far. It is currently only looking in the keywords custom field:
elseif($program_search) {
// search by program search text
query_posts(array(
'post_type' => 'program',
'meta_query' => array(
array(
'key' => 'keywords',
'value' => $program_search,
'compare' => 'LIKE'
),
)
));
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
$l.= "<div class='program-item'>";
$l.= "<div class='program-item-image'><a href='".get_permalink($post->ID)."'>". get_the_post_thumbnail($post->ID, 'thumbnail')."</a></div>";
$l.= "<div class='program-item-title'><a href='".get_permalink($post->ID)."'>".get_the_title($post->ID)."</a></div>";
$l.= "<div class='program-item-content'>".get_the_excerpt()."</div>";
$l.= "<div style='clear:both;'></div>";
$l.= "</div>";
endwhile;
else:
endif;
}
First, don’t use
query_posts
.Second, you can pass an
s
parameter to get most of the way there.That
s
parameter kicks the ordinary search mechanisms into place and the title and the content gets searched. If you look at that generated query you will see…That is most of what you want. The
LIMIT
unless otherwise specified is the limit set at wp-admin->Settings->General. There is a problem though.I am pretty sure you want that to be
OR ((wp_postmeta.meta_key ...
and you really want it up with thepost_title
and thepost_content
too. Something like this:WP_Query
won’t do that so we have to make it with some filters. Proof of concept:Notice that I left out the
meta_query
altogether and largely duplicated the functionality. That is to keep that troublesomeAND
from being generated.You are applying and immediately removing those filters so they do not interfere with any other queries. There are other ways to keep the filter out of the way or other queries. One such method is outlined here. You can also add the
remove_filter
to theadd_filter
callback to have them automatically remover themselves.