Limit Search Results to Blog Posts Only

Community Forums Forums Design Tips and Tricks Limit Search Results to Blog Posts Only

This topic is: not resolved

This topic contains 5 replies, has 2 voices, and was last updated by  Genesis Developer 1 year, 2 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #78098

    jwd
    Participant

    Is there any way to limit the search widget results to blog posts only? I do not want any pages included in the results.
    Thanks!

    #78100

    Genesis Developer
    Participant

    Try this

    add_action( ‘pre_get_posts’, ‘custom_post_limit’ );
    function custom_post_limit( $query ) {

    if( $query->is_serach && (get_post_type() == ‘post’) ) {
    $query->set( ‘posts_per_page’, ‘PUT YOUR LIMIT HERE’ );
    }

    }


    Need help for Genesis Websites, contact me | Checkout my PRO Genesis Child Theme .

    #78106

    jwd
    Participant

    Put this in functions.php. Put limit in and got a parse error. :-(

    #78107

    Genesis Developer
    Participant

    sorry! typo mistake

    use this

    add_action( ‘pre_get_posts’, ‘custom_post_limit’ );
    function custom_post_limit( $query ) {

    if( $query->is_search && (get_post_type() == ‘post’) ) {
    $query->set( ‘posts_per_page’, ‘PUT YOUR LIMIT HERE’ );
    }

    }


    Need help for Genesis Websites, contact me | Checkout my PRO Genesis Child Theme .

    #78132

    jwd
    Participant

    Thanks. Tried this, no error, but the pages are still showing in the results.

    #78374

    Genesis Developer
    Participant

    Ok. remove the above function and use this code

    add_action( 'pre_get_posts', 'only_blog_post' );
    function only_blog_post( $query ) {
    
        if( $query->is_search ) {
              $query->set( 'post_type', array('post') );
        }
    
    }

    Need help for Genesis Websites, contact me | Checkout my PRO Genesis Child Theme .

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.