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  genwrock 10 months, 2 weeks ago.

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

    jwd
    Participant
    Post count: 4

    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

    genwrock
    Member
    Post count: 689

    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’ );
    }

    }


    • This reply was modified 10 months, 2 weeks ago by  genwrock.
    • This reply was modified 10 months, 2 weeks ago by  genwrock.
    #78106

    jwd
    Participant
    Post count: 4

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

    #78107

    genwrock
    Member
    Post count: 689

    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’ );
    }

    }


    #78132

    jwd
    Participant
    Post count: 4

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

    #78374

    genwrock
    Member
    Post count: 689

    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') );
        }
    
    }

    • This reply was modified 10 months, 2 weeks ago by  genwrock.
    • This reply was modified 10 months, 2 weeks ago by  genwrock.
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.