Skip to main content
added 26 characters in body
Source Link
Shane
  • 821
  • 6
  • 13

I believe something like this would do the trick.

EDIT

My code assumes the querystring value is the post_type, not an integer like your form field. Also, your form field is named 'cat', I think WordPress has that reserved, at least I remember having conflicts when I last used 'cat' in a querystring.

EDIT 2

(Changed filter from 'posts_where' to 'pre_get_posts' after having done some tests)

In functions.php

 function my_filter( $where, $query )
 {
      if( is_search() )
      {
           $type = ( !empty( $_GET['your-field'] ) ) ? $_GET['your-field'] : "";
           
           if( $type )
                $query->set( 'post_type', $type );
      }
 }
 add_filter( 'posts_where''pre_get_posts', 'my_filter', 10, 2 );

I believe something like this would do the trick.

EDIT

My code assumes the querystring value is the post_type, not an integer like your form field. Also, your form field is named 'cat', I think WordPress has that reserved, at least I remember having conflicts when I last used 'cat' in a querystring.

In functions.php

 function my_filter( $where, $query )
 {
      if( is_search() )
      {
           $type = ( !empty( $_GET['your-field'] ) ) ? $_GET['your-field'] : "";
           
           if( $type )
                $query->set( 'post_type', $type );
      }
 }
 add_filter( 'posts_where', 'my_filter', 10, 2 );

I believe something like this would do the trick.

EDIT

My code assumes the querystring value is the post_type, not an integer like your form field. Also, your form field is named 'cat', I think WordPress has that reserved, at least I remember having conflicts when I last used 'cat' in a querystring.

EDIT 2

(Changed filter from 'posts_where' to 'pre_get_posts' after having done some tests)

In functions.php

 function my_filter( $query )
 {
      if( is_search() )
      {
           $type = ( !empty( $_GET['your-field'] ) ) ? $_GET['your-field'] : "";
           
           if( $type )
                $query->set( 'post_type', $type );
      }
 }
 add_filter( 'pre_get_posts', 'my_filter' );
added 108 characters in body
Source Link
Shane
  • 821
  • 6
  • 13

I believe something like this would do the trick.

EDIT

My code assumes the querystring value is the post_type, not an integer like your form field. Also, your form field is named 'cat', I think WordPress has that reserved, at least I remember having conflicts when I last used 'cat' in a querystring.

In functions.php

 function my_filter( $where, $query )
 {
      if( is_search() )
      {
           $type = ( !empty( $_GET['your-field'] ) ) ? $_GET['your-field'] : "";
           
           if( $type )
                $query->set( 'post_type', $type );
      }
 }
 add_filter( 'posts_where', 'my_filter', 10, 2 );

I believe something like this would do the trick.

In functions.php

 function my_filter( $where, $query )
 {
      if( is_search() )
      {
           $type = ( !empty( $_GET['your-field'] ) ) ? $_GET['your-field'] : "";
           
           if( $type )
                $query->set( 'post_type', $type );
      }
 }
 add_filter( 'posts_where', 'my_filter', 10, 2 );

I believe something like this would do the trick.

EDIT

My code assumes the querystring value is the post_type, not an integer like your form field. Also, your form field is named 'cat', I think WordPress has that reserved, at least I remember having conflicts when I last used 'cat' in a querystring.

In functions.php

 function my_filter( $where, $query )
 {
      if( is_search() )
      {
           $type = ( !empty( $_GET['your-field'] ) ) ? $_GET['your-field'] : "";
           
           if( $type )
                $query->set( 'post_type', $type );
      }
 }
 add_filter( 'posts_where', 'my_filter', 10, 2 );
Source Link
Shane
  • 821
  • 6
  • 13

I believe something like this would do the trick.

In functions.php

 function my_filter( $where, $query )
 {
      if( is_search() )
      {
           $type = ( !empty( $_GET['your-field'] ) ) ? $_GET['your-field'] : "";
           
           if( $type )
                $query->set( 'post_type', $type );
      }
 }
 add_filter( 'posts_where', 'my_filter', 10, 2 );