Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1228227
    keith s
    Participant

    I used some code from another thread to add a sidebar to single product pages in my template layout. I inserted the code into my child theme functions.php file and it worked great. The only issue is it now creates an “invalid post type” error when searching products in the dashboard. The first search after you refresh will work and then subsequent searches produce the error. I tested it by removing the code and the product search mechanism works fine.

    Please advise. Thank you.

    http://www.midweststeeringdev.com
    All versions of x theme, WP and related plugins are up to date.

    *I will follow up with credentials in a private message.

    #1228234
    keith s
    Participant
    This reply has been marked as private.
    #1228293
    Thai
    Moderator

    Hi There,

    Please find this code:

    add_filter( 'pre_get_posts', 'custom_search' );
    function custom_search( $query ) {
        if ( $query->is_search ) {
            $query->set( 'post_type', array( 'product' ) );
        }
        return $query;
    }

    And change to this:

    add_action( 'pre_get_posts', 'custom_search' );
    function custom_search( $query ) {
    
    	if(is_admin()){
    		return $query;
    	}
    
        if ($query->is_main_query() && $query->is_search ) {
            $query->set( 'post_type', array( 'product' ) );
        }
    
        return $query;
    }

    Hope it helps 🙂

    #1228338
    keith s
    Participant

    You’re awesome! Works great! Thank you.

    #1228427
    Thai
    Moderator

    Glad we’re able to help 🙂

    If you need anything else please let us know.

  • <script> jQuery(function($){ $("#no-reply-1228227 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>