• Resolved powerj

    (@powerj)


    I can’t work out what is wrong with this code – is_page(61) equates to true (by itself) and it works as desired if I take out the is_page(61)

    //* Add search box to specific page sidebars
    add_action( 'pre_get_posts', 'wt_pre_get_posts_nav_search' );
    //-------------------------------
    function wt_pre_get_posts_nav_search($query) {
        //Use CPT mediaReleases if page is Media Releases
        if (is_page( 61 )) { //if Media Releases Page
            if ($query->is_search && !is_admin()  )  {
                $query->set('post_type', 'mediaReleases');
            } 
        }
    }

    Or I tried this

    //* Add search box to specific page sidebars
    add_action( 'pre_get_posts', 'wt_pre_get_posts_nav_search' );
    //-------------------------------
    function wt_pre_get_posts_nav_search($query) {
        //Use CPT mediaReleases if page is Media Releases
            if ($query->is_page( 61 ) && $query->is_search && !is_admin()  )  {
                $query->set('post_type', 'mediaReleases');
            } 
    }

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • is_page() will tell you the page you’re on, not the page you searched from. By the time pre_get_posts runs, the search results are being loaded, not page 61.

    If you want to search a particular post type, I suggest creating your own search form and adding a hidden input for post_type with a value mediaReleases.

    <input name="post_type" type="hidden" value="mediaReleases">
    
    Thread Starter powerj

    (@powerj)

    Ahh OK, thanks

    • This reply was modified 7 years, 7 months ago by powerj.
    Thread Starter powerj

    (@powerj)

    I’ve come up with this code for the underlying template, which is working fine except for not restricting to the custom post type

    /*Limit search box to search in cpt media releases*/
    add_filter( 'genesis_search_form', 'wt_search_form');
    function wt_search_form( $form ) {
        $homeUrl =  home_url( '/' );
        
    		$form = '<form role="search" method="get" class="search-form" action="' . $homeUrl . '">
            <label><span class="screen-reader-text">Search for Media Releases:</span><input type="search" class="search-field" placeholder="Search Media Releases" value="" name="s" title="Search for Media Releases:" /></label>
            <input type="hidden" name="post_type" value="mediaReleases"><input type="submit" class="search-submit" value="Search" /></form>
    		</form>';
    		return $form;
    	}
    Thread Starter powerj

    (@powerj)

    Oh you’re kidding me! The reason this wasn’t working is because of the camel case custom post type mediaReleases
    The post type was registered as
    register_post_type( 'mediaReleases', $args );

    I changed it to
    register_post_type( 'mediareleases', $args );

    and changed the filter function and now it works!

    If anyone can explain why using camel case in the instance doesn’t work, I would appreciate it.

    (For transparency I posted this on the Studio Press forums also – because I’m using the Genesis framework)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search Box Filter via pre_get_posts’ is closed to new replies.