0

I'm using the following code to limit my search to products only (searching in the product archive).

// Only show products in the front-end search results
add_filter('pre_get_posts','lw_search_filter_pages');
function lw_search_filter_pages($query) {
// Frontend search only
if ( ! is_admin() && $query->is_search() ) {
    $query->set('post_type', 'product');
    $query->set( 'wc_query', 'product_query' );
}
return $query;
}

What I'm trying to accomplish, since we have a blog on our page, that when a searchquery is fired from the /blog page or blogposts, that the normal blog search is used.

I've tried to add the if statement:

//Only show products in the front-end search results
function lw_search_filter_pages($query) {
    if ( ! is_admin() && $query->is_search() ) {
        if($wp_query->query['pagename']=='blog'){
        }
        else {
        $query->set('post_type', 'product');
        $query->set( 'wc_query', 'product_query' );
        }
    }
    return $query;
}

Without success. How do i proplery add the IF-Statement for blogpage & blogposts only? I've tried to add an if statement to the query if its not the pagename blog (though here we would miss the posts.

1
  • Also tried it with if ( ! is_home() ) { , but that didn't work out either – Nicolas Körner Apr 24 at 15:01
0

I don't fully understand, but:

function lw_search_filter_pages ( $query ) {

    // ...

}

if ( is_front_page () || is_home () ) {} else {

    add_filter ( 'pre_get_posts', 'lw_search_filter_pages' );

}

Another:

function lw_search_filter_pages ( $query ) {

    // ...

}
                                   // https://yoursite.tld/blog
$uri = $_SERVER [ 'REQUEST_URI' ]; //             $uri = '/blog';

if ( $uri == '/blog' ) {} else {

    add_filter ( 'pre_get_posts', 'lw_search_filter_pages' );

}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.