• Hi there. I’ve been working on my page’s searching for a few days now. I’ve finally found some info on how to progress (I had been stuck for a few days), but I’ve run into another problem…

    Basically what I want to do is have searches show results (pages and/or posts) generated by the searched term(s) matching EITHER the page’s/post’s title/content (done automatically by WordPress’ default search) OR the post’s tags.

    For example, the user types “awesome” and searches for it. The results would show pages/posts that have “awesome” in their title, in their content, OR in their tags.

    What I have as my searching method now is this:

    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    
    $wp_query->query('showposts='.$resultsPerPage.'&paged='.$paged.'&orderby='.$sortBy.'&s='.$s);
    
    while ($myArray->have_posts()) : $myArray->the_post();
        $title = get_the_title();
        $content = get_the_content();
        echo '<h2>' . $title . '</h2>';
        echo $content;
    endwhile;
    include (TEMPLATEPATH . '/inc/nav.php' );
    $wp_query->query('showposts='.$resultsPerPage.'&paged='.$paged.'&orderby='.$sortBy.'&s='.$s);

    ^This line^ being what’s doing the searching (if I understand correctly). So it will give me results that contain the search term(s) stored in $s in their title or content, order them by whatever is stored in my $sortBy variable (by default I have it as ‘date’), show how ever many posts according to the $resultsPerPage variable (by default I have it as 11), and paginate the results (if necessary).

    NOW… If I include the ‘tag’ parameter in that line:

    $wp_query->query('showposts='.$resultsPerPage.'&paged='.$paged.'&orderby='.$sortBy.'&s='.$s.'&tag='.$tags);

    where $tags is a string of the search term(s) ($tags = str_replace(” “, “,”, $s);), then it will show results that have the search term(s) in BOTH their title/content AND their tags.

    So, for example, the user searches “awesome.” The only results will be posts that have “awesome” in both the title/content AND the tags. If a post has “awesome” in its content but not its tags, it will not show; if a post has “awesome” in its tag but not its content/title, it will not show. Again, I’d like to show results that have “awesome” in EITHER the title/content OR the tags.

    At first, I thought just replacing the & with | (or) before ‘tag’ in the query parameters would do it, but apparently that doesn’t seem to work (nor do I think it’s proper code for the arguments).

    I’ve also tried having two queries–one that searches for the search term(s) in the title/content with ‘s=’.$s and another that searches for the search term(s) in the tags with ‘tag=’.$tags, combining the two queries, then removing duplicate posts. I’ve tried this a few different ways and haven’t gotten any of them to work…

    Is there any way to do what I’m trying to do? The simpler the answer, the better (this is my first time using WordPress and PHP) :P. Also, I’m worried about using something other than $wp_query->query($args); because I was having trouble paginating results when I used other methods such as query_posts or get_posts. I don’t know if that matters in the end, but that’s why I’ve been hesitant to change it.

    I hope that all makes sense. Thanks in advance for the help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Sizukhan

    (@sizukhan)

    Okay, well I think I have it working with MySQL, but now I need help paginating the results. I have:

    $querystr="
        SELECT *
            FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->term_taxonomy, $wpdb->terms
                WHERE ($wpdb->terms.name = '$s'
                OR $wpdb->posts.post_content LIKE '%$s%'
    	    OR $wpdb->posts.post_title LIKE '%$s%')
    	    AND $wpdb->posts.ID = $wpdb->term_relationships.object_id
    	    AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
    	    AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id
    	    ORDER BY $wpdb->posts.post_date DESC
    ";
    
    $pageposts = $wpdb->get_results($querystr, OBJECT_K);
    
    foreach ($pageposts as $post): setup_postdata($post);
        echo the_title() . '<br /><br />';
    endforeach;

    So (unless I’m missing something), this prints the titles of any posts that have the search term(s) in their tags, titles, or contents!

    But again, the problem now is that I have no idea how to paginate my results…

    Also (I’m new to pretty much all of this), is this code creating anything in the database that needs to be deleted? Like am I (unknowingly to me with my limited knowledge on the subject) creating extra tables or something that permanently take up space on the database?

    Any help would be greatly appreciated!

    Hi Sizukhan,

    Did you make any more progress with this? I am about to embark on a similar search customisation — I want to return results that contain the search term EITHER in custom post type pages, OR in a certain blog category.

    Were you able to get pagination working?

    Best,

    David

    Thread Starter Sizukhan

    (@sizukhan)

    I did! However, it’s been a few months since I looked at the code, so what I’m about to post may or may not be enough to get it working for you.

    So basically, after I finish adding variables and such to $querystr, I have these lines:

    $pageposts = $wpdb->get_results($querystr, OBJECT_K); //put the query results into the $pageposts variable
    $wp_query->found_posts = count($pageposts);  //set the number of found posts
    $wp_query->max_num_pages = ceil($wp_query->found_posts/$resultsPerPage);  //calculate the number of pages needed to display all of the reults based on the number of pulled posts and the $resultsPerPage
    $on_page = intval(get_query_var('paged'));  //get the current page number
    //if, for some reason, the $on_page variable = 0 (which it should NEVER be), reset the varaible to page 1
    if($on_page==0)
    {
    	$on_page=1;
    }
    $offset = ($on_page-1)*$resultsPerPage;  //calculate the offset (where the page will start displaying posts in the returned array
    $querystr .= "LIMIT $resultsPerPage OFFSET $offset";  //add the LIMIT and OFFSET parameters to the SQL query
    $pageposts = $wpdb->get_results($querystr, OBJECT_K);  //query again with the newly added LIMIT and OFFSET parameters

    Then I have the foreach loop that sets up the CSS and whatever else for each result returned:

    //format each displayed result
    foreach ($pageposts as $post): setup_postdata($post);
    //...setting up CSS and stuff...
    endforeach;

    And, finally, I have code for the paginated links that appear at the bottom of the page:

    $big = 999999999; // need an unlikely integer
    $pageNumber = (get_query_var('paged')) ? get_query_var('paged') : 1;
    echo '<div>' . paginate_links( array('base' => str_replace( $big, '%#%', get_pagenum_link( $big )), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged')),'total' => $wp_query->max_num_pages, 'end_size'=>2, 'mid_size'=>2, 'prev_text'=>'<', 'next_text'=>'>')) . '</div>';

    And that’s it (I think)! Let me know if that helps 🙂

    Thanks for the reply, Sizukhan!

    The site for which I was going to implement this has been put on the backburner for a while, but I’ve got this thread bookmarked ready to use when I need it. From looking through the code, it looks like it will work a treat.

    Many thanks again for taking the time to update the thread. I’m sure other people will find this useful too.

    Best,

    David

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show Search Results from EITHER Content/Titles OR Tags’ is closed to new replies.