public
Last active

Wordpress : Custom search page and search by title, content and tag

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?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>';
}
}
}
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.