Search Widget

  • sylvia

    #2721

    Is there a way to limit the search widget to only searching posts……and not pages. Almost every type of search that I try on my site usually includes my “Terms of Service” page in the search results. I only want the search results to display post content and not page content…is this possible?

    Prasanna SP

    #21629

    Put this to your child theme’s functions.php file (If you don’t know how to modify child theme functions, you may read this – How to modify the theme using a child theme)

    function searchfilter($query) {

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

    return $query;
    }

    add_filter('pre_get_posts','searchfilter');

    soapy

    #21630

    Prasanna,

    THANK YOU!

    Prasanna SP

    #21631

    Did that work?? If it’s working, please mark this thread as resolved..

    soapy

    #21632

    I’m just uploading right now….I’ll let you know in 5 mins….

    Prasanna SP

    #21633

    Okay, what’s the URL of your site?

    soapy

    #21634

    No…it did not work….here is a copy of my functions.php file. This is the first time I made a functions.php file to put in my child theme folder so i’m not sure if i did this correctly…..

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    function searchfilter($query) {

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

    return $query;
    }

    add_filter('pre_get_posts','searchfilter');

    </body>
    </html>

    soapy

    #21635

    It actually started displaying the code all over my dashboard in wordpress…..

    Prasanna SP

    #21636

    No! it’s not the HTML code. It’s php. So put codes between <?php and ?>

    Put this in functions.php

    <?php
    function searchfilter($query) {

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

    return $query;
    }

    add_filter('pre_get_posts','searchfilter');
    ?>

    soapy

    #21637

    ok give me a minute here and i’ll let u know…thanks again…

Viewing 10 posts - 1 through 10 (of 12 total)

You must be logged in to reply to this topic.