• So, i created a custom search using WP_Query and it is working as i need it to except for the fact that it does not seem to be searching in page tags (i have a custom plugin that allows me to add tags to pages which i implemented specifically for the search function).

    Essentially i created a new instance of the WP_Query class in a new file (php) in my theme’s default folder and am calling this php file in the default “search.php” file which renders the search results.

    
    <?php load_template(TEMPLATEPATH . '/internal-search.php'); ?>
    

    The following is the code i am using in the new custom php file (internal-search.php)

    
    <header call="page=header">
                <h1><?php _e( 'Categories Found For', 'locale' ); ?>: "<?php the_search_query(); ?>"</h1>
         </header>
    
    <?php 
    // the query
    $the_query = new WP_Query( array( 'post_type' => 'page' , 'post_status' => 'publish' , 'post_parent' => 83 , 's' => get_search_query() ) ); ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
        <!-- the loop -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <?php the_title( sprintf( '<h2 class="title-post entry-title"><a href="%s" rel="bookmark noopener noreferrer" target="_blank">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
        <?php endwhile; ?>
        <!-- end of the loop -->
    
        <?php wp_reset_postdata(); ?>
    
    <?php else : ?>
            <p><?php _e( 'Sorry, no categories matched your criteria.' ); ?></p>
    <?php endif; ?>
    

    This might be a stupid question or something i am missing in the code, but how can i modify this search to also output results based on the query keyword matching the tags of the different pages?

    Thanks

    • This topic was modified 5 years, 7 months ago by mportelli.
    • This topic was modified 5 years, 7 months ago by mportelli.
    • This topic was modified 5 years, 7 months ago by mportelli.
    • This topic was modified 5 years, 7 months ago by Yui. Reason: please use CODE button for code formatting
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You need to add the “tax_query” argument to WP_Query instantiation. Find the “taxonomy parameters” section on the WP_Query docs page for more information on using it. (A direct link to the right section doesn’t land in the right place due to hidden content.)

    Thread Starter mportelli

    (@mportelli)

    Thanks for the feedback @bcworkz

    I’ve reviewed the WP_Query docs page a number of times and have tried the inclusion of a number of extra parameters as well as removal of some the ones i have just in case they were too limiting.

    What value would i use for the “tax_query” argument though since i don’t want to limit search to specific tags but instead want the search to include all tags when running the query and therefore return pages where the tag is like get_search_query()?

    • This reply was modified 5 years, 7 months ago by mportelli.
    Moderator bcworkz

    (@bcworkz)

    Sorry, I don’t understand your precise goals so I’m not sure how to advise further. You say you “don’t want to limit search to specific tag” but “return pages where the tag is like get_search_query() [like the search terms]”. Isn’t that limiting results to specific tags? If you want results to disregard tags, there’s no need to be more specific with the query. But if you want to include posts with tags matching search terms then “tax_query” needs to be used.

    I think I see the confusion. It’s whether the “tax_query” parameter should AND’d or OR’d with the rest of the query. WP will use AND logic such that results have to have one of the tags matching one of the search terms. If you want OR logic so posts with such tags are included but other search matches without any tag matches are also included, the resulting SQL needs to be modified through the “posts_request” filter to change the crucial AND to OR.

    Thread Starter mportelli

    (@mportelli)

    My apologies, what i meant was that i don’t want the search results to be constantly limited to a specific tag (e.g. tag = shoes) but instead i want the search to be dynamic such that it will allow searching of only those tags that match the search term (i.e. tag like the_search_query() ).

    Just to clarify, you are suggesting the usage of the “tax_query” parameter within the search array, such as the example below?

    $the_query = new WP_Query( array( 'post_type' => 'page' , 'post_status' => 'publish' , 'post_parent' => 83 , 's' => get_search_query() , 'tax_query' => get_search_term() ) ); ?>

    Thanks

    • This reply was modified 5 years, 7 months ago by mportelli.
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Custom WP search not looking at page tags’ is closed to new replies.