How to include/exclude posts matching a custom taxonomy term from search results

Hi,

I have a custom content type and custom taxonomies set up on a site which is working very smoothly except for filtering out some results that match a specific taxonomy term in some of my search results that have a custom results template.

See http://handheld.simplecsr.com/test-historic/

I have a custom code in my child theme functions php (i will inlcude this at the bottom) which forces the behavior of the search form on this page when it first loads.

It displays only results matching the Route Type > Historic Home Walks on first page load.

However on second search it includes all matching types (not limited to the matching term)

The results have their own custom template which has the line:

<?php while ( $gmw_query->have_posts() ) : $gmw_query->the_post(); ?>

to produce the results.

MY QUESTION

IS THERE A QUERY I CAN ADD TO PREVENT POSTS FROM OTHER TERMS SHOWING?

<?php [MYQUERYHERE] : while ( $gmw_query->have_posts() ) : $gmw_query->the_post(); ?>

SOMETHING LIKE:

$the_query = new gmw_query( array( 'post_in' => get_option( 'historic-home-walks' ) ) );
if ( $the_query->have_posts() )

(please note my custompress post type name is "walk")

Anything I have tried so far has not worked.

ADDITIONAL DETAILS

The original code I am using on first page load to rproduct these intial results is:

`//will control only form with ID 7

if ( $gmw->form['ID'] == 7 ) {

//set post type

$gmw->form['search_form']['post_types'] = array('walk');

//remove all taxonomy queries created by GEO my WP or other add-ons

remove_all_filters( 'gmw_pt_tax_query' );

//create your own tax query

function gmw_tax_query( $tax_args, $gmw ) {

$tax_args = array(

array(

'taxonomy' => 'route_type',

'field' => 'slug',

'terms' => 'historic-home-walks'

)

);

return $tax_args;

}

add_filter( 'gmw_pt_tax_query', 'gmw_tax_query', 99, 2 );

//submit results

$gmw->form_submitted();

}`

  • Michael Bissett

    Hey @chris, hope you're doing well today. :slight_smile:

    I'll have to send this off to our more code savvy guys in SLS, as this sounds like a task more suited to them.

    In the meantime though, could you re-post the code from your functions.php in a comment here? Looks like there was a formatting hiccup earlier, and it'd help us to get this fixed if it was a little easier to read.

    Thanks!

    Regards,
    Michael

  • chris

    Hi Michael,

    This is the relevant code from the functions.php file. It is controlling the initial set of results only.

    <?php
    
    function gmw_tax_based_results( $gmw ) {
    
    	if ( isset( $gmw->form['in_widget'] ) )
    
    		return;
    
    	//will control only form with ID 7
    
    	if ( $gmw->form['ID'] == 7 ) {
    
    		//set post type
    
    		$gmw->form['search_form']['post_types'] = array('walk');
    
    		//remove all taxonomy queries created by GEO my WP or other add-ons
    
    		remove_all_filters( 'gmw_pt_tax_query' )
    
    		//create your own tax query
    
    		function gmw_tax_query( $tax_args, $gmw ) {
    
    			$tax_args = array(
    
    					array(
    
    							'taxonomy'  	=> 'route_type',
    
    							'field' 	=> 'slug',
    
    							'terms' 	=> 'historic-home-walks'
    
    					)
    
    			);
    
    			return $tax_args;
    
    		}
    
    		add_filter( 'gmw_pt_tax_query', 'gmw_tax_query', 99, 2 );
    
    		//submit results
    
    		$gmw->form_submitted();
    
    	}
    
    }
    
    add_filter( 'gmw_main_shortcode_custom_function',  'gmw_tax_based_results', 99 );

    END

    Many thanks
    Chris

  • chris

    Hi Ash,

    I hope you are well. The large piece of code works properly -but it only effects the initial results on page load. So far I have not made any update to the results file.

    I have tried to replace:

    <?php while ( $gmw_query->have_posts() ) : $gmw_query->the_post(); ?>

    with

    <?php
    $the_query = new gmw_query( array( 'post__in' => get_option( 'historic-home-walks' ) ) );
    if ( $the_query->have_posts() ) : while ( $gmw_query->have_posts() ) : $gmw_query->the_post();
    ?>

    But this creates a syntax error I cant resolve. (Parse error: syntax error, unexpected $end in..... suggesting something is left open???

    The aim is to restrict the results to only search for my custom post type of walks that match the "historic Home Walks" term in my custom taxonomy "Route Type"

    The ftp access you had for the site before is still active if you want to check out the results file.) It is in /public_html/handheld/wp-content/themes/Divi-child/geo-my-wp/posts/search-results/handheld

    Many thanks
    Chris

  • chris

    Hi Ash,

    (Please see my other response above as well as this message...)

    Today i received these details for the developer of the plugin that generates the actually results from the form on that page. This may be why i havent been able to force any results here???

    His comment is:

    "The declaration of the WP_query is happening before the results.php file. However, there is a filter that you could use in order to modify the wp_query.

    open the file geo-my-wp/plugins/posts/includes/gmw-pt-search-functions.php and around line 705 you will see the filter

    apply_filters( 'gmw_pt_form_before_posts_query', $this->form, $this->settings );

    try using this filter."

    I need it to expressly refer to this form in the filter (as i need to duplicate this process for other forms with other set of results so will be identifying other forms.)

    Any idea how to apply this filter?

    Thanks Chris

  • Ash

    That's great so far.

    To apply this filter you need to use like this:

    add_filter( 'gmw_pt_form_before_posts_query', 'my_new_form_filter' );
    function my_new_form_filter( $form, $form_settings ) {
    // your code will go here
    }

    Now inside of the my_new_form_filter function you can try one of the following:

    print_r($form_settings);
    print_r($form);

    And then check which one contains the query for search. Then you can alter the query inside the function, and finally return the variable.

    For example, if you see $form contains the query, then change the query to your own and at last:

    return $form;

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • chris

    Hi Ash,

    I haven't worked with any filters like this before and am not sure about how or where to make these changes.

    The file geo-my-wp/plugins/posts/includes/gmw-pt-search-functions.php will get overwritten on update, unlike the functions.pho in my child theme or the custom results.php file in /public_html/handheld/wp-content/themes/Divi-child/geo-my-wp/posts/search-results/handheld which are both protected from updates.

    I believe it should be possible to just add the code to the results.php as per my post on THU JUN 26 2014, 10:02:23 AM.

    This way it would allow me to create additional custom results.php for each separate search form.

    The search forms I refer to are those used on four main areas on the site for searching each of the four route type terms. (pub-walks, waterway-walks, historic-home-walks, countryside-walks)

    Each form has its own unique ID and a separate results.php file can be specified for each.

    Sorry if I am confusing things... i am stuck with this.

    Thanks
    Chris

  • Ash

    Hello Chris,

    As you are using a custom theme's functions so I am not clearly sure about the procedure - how they are handling the wp query etc. You said in your earlier post that the original developer showed you a filter, so that is the way I have shown how to use that filter.

    Also, in cases like this, always the developer is the best person to suggest with. If you need to any code to get posts from specific taxonomy of a custom post type, I can help you with that. But as you are using custom function, I am not sure I am afraid.

    Anyway, I am tagging an available developer from second level support line in this thread for his valuable opinion on this issue.

    Cheers
    Ash

  • chris

    Hi Ash,

    That's all understood and is what i was trying to avoid.

    The results.php file I was trying to edit has the query:

    <?php while ( $gmw_query->have_posts() ) : $gmw_query->the_post(); ?>

    Which i planned to change to:

    <?php
    $the_query = new gmw_query( array( 'post__in' => get_option( 'historic-home-walks' ) ) );
    if ( $the_query->have_posts() ) : while ( $gmw_query->have_posts() ) : $gmw_query->the_post();
    ?>

    But this is throwing out an error.

    But when i made the change using the code above i received a syntax error I cant resolve. (Parse error: syntax error, unexpected $end in..... suggesting something is left open??? but i cant find the cause.)

    Perhaps if someone can check this out for me (i have tried multiple times without success.)

    I am not convinced the developer has not misunderstood my requirement and infact the change to display selected custom posts should behave like standard wordpress.

    Thanks again
    Chris

  • Saurabh

    Hi @chris,

    Hope you are well today!

    The code that you are trying to use doesn't work because, by the time your taxonomy filters, etc are applied, the query has already been formed. You won't be able to modify it.

    As the developers have suggested, you'd need to use a filter that is applied before the query is formed.

    What @Ashok and the developers have suggested previously is what you'd need to do. What we are missing here is that the $this->form returned by the filter is the same as $gmw->form that you've been using. So, the function you'd be using would be something like this:

    add_filter( 'gmw_pt_form_before_posts_query', 'my_new_form_filter' );
    
    function my_new_form_filter( $form ) {
    
        // if isn't from widget, return
        if ( isset( $form['in_widget'] ) ){
            return;
        }
    
        // if isn't form id 7, return
        if ( $form['ID'] != 7 ) {
            return;
        }
    
        // we reached here, continue
    
        // custom tax query
        $tax_args = array(
            array(
                'taxonomy' => 'route_type',
                'field' => 'slug',
                'terms' => 'historic-home-walks'
            )
        );
    
        // modify form
        $form['query_args']['post_type'] = array('walk');
        $form['query_args']['tax_query'] = $tax_args;
    
        // return modified form
        return $form;
    }

    See http://plugins.svn.wordpress.org/geo-my-wp/tags/2.4.6.3/plugins/posts/includes/gmw-pt-search-functions.php and find function results() on the page for more details.

    I hope this helps. Do let us know if you need more help with it.

    Best

    Saurabh

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.