Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@l2aelba
Last active August 18, 2020 18:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l2aelba/4450989 to your computer and use it in GitHub Desktop.
Save l2aelba/4450989 to your computer and use it in GitHub Desktop.
Wordpress : Custom search page and search by title, content and tag
<?php
include('../../../../wp-config.php');
$keyword = $_GET['k'];
search($wpdb->get_results("SELECT * from wp_posts WHERE post_title LIKE '$keyword%'"), "Full keyword");
$keywords_split = explode(" ",$keyword);
for($i=0;$i<count($keywords_split);$i++){
search($wpdb->get_results("SELECT * from wp_posts WHERE post_title LIKE '$keywords_split[$i]%'") ,"Split keyword = '$keywords_split[$i]'");
search($wpdb->get_results("SELECT * from wp_posts WHERE post_content LIKE '%$keywords_split[$i]%'") ,"Split keyword = '$keywords_split[$i]' (Content)");
}
$tags = str_replace(" ","-",$keyword).",".str_replace(" ",",",$keyword);
$searchtags = new WP_Query(array('post_type'=>array('one','two','three') ,'tag' => $tags));
while($searchtags->have_posts()):
$searchtags->next_post();
echo '<li>' . get_the_title($searchtags->post->ID ) . ' tag</li> ';
endwhile;
function search($results,$mark){
foreach($results as $data){
if($data->post_status =='publish'){
echo '<li>#'.$data->ID.' '.htmlentities($data->post_title).' ('.$mark.')</li>';
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment