I would like:
http://www.gadgetgogo.co.uk/?s=ipod
to return as:
http://www.gadgetgogo.co.uk/?s=ipod&post_type=product
So when using searches (slider banner and default WordPress search) it produces the second URL.
I was looking for similar solution as you but couldn't find any and at last I combined last few answers that I read here and got this working fine as I wanted.
Add below code in your theme's functions.php file
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) && ($_GET['post_type'] != 'product') ) {
wp_redirect( home_url( "/?s=" ) . urlencode( get_query_var( 's' ) ) . "&post_type=product" );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
hope it will help you and others.
Apparently current WooCommerce versions only consider a search query a product search (and render using the appropriate template), if $query->is_post_type_archive( 'product' ) is true, so the key is to set not only the post_type, but the is_post_type_archive property as well, and to do it before WooCommerce loads its filter (default priority of 10), so with a priority of 9 or smaller.
Example to add into funtions.php:
function my_search_filter($query) {
if ( $query->is_search && ! is_admin() ) {
$query->set( 'post_type', 'product' );
$query->is_post_type_archive = true;
}
}
add_filter('pre_get_posts','my_search_filter', 9);
Please note, that this code will override all searches as product serach, so if you have other searches as well, implement appropriate checks at the begining of my_search_filter.
Just add this line to top of search.php
$_GET['post_type'] = 'product'
This can be done using pre_get_posts filter. Add below code in your theme's functions.php file
add_filter( 'pre_get_posts', 'search_by_product_only' );
function search_by_product_only( $query ) {
// check if search query only
if ( $query->is_search ) {
$query->set( 'post_type', array( 'product') ); // here you can add multiple post types in whcih you want to search
}
return $query;
}
Related
I am trying to hide a few pages from WordPress Search results. For that, I have written a code.
function jp_search_filter( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( 'post__not_in', array( 10,11,20,105 ) );
}
}
add_action( 'pre_get_posts', 'jp_search_filter' );
However, this code doesn't seem to be working. The pages that I wanted to hide from WordPress search results are still showing. What I have done in the above code is I passed the page IDs as an array.
Can anyone help me with the code? Thank you so much.
I found this PHP code:
add_action( 'pre_get_posts', 'generate_random_category_posts', 100 );
function generate_random_category_posts( $query ) {
$catto = get_queried_object();
if ( $query->is_category() && $query->is_main_query() && $catto->term_id === 9 ) {
$query->set( 'orderby', 'rand' );
}
}
The person was able to randomly display WordPress posts for the chosen category (9, in this case). What I would like to do is randomly display all categories except a single chosen category.
I know if I remove
IF (if ( $query->is_category() && $query->is_main_query() &&
$catto->term_id === 9 )
it will randomly displays all categories and not just a single one (which is OK), but how would I do it to exclude a single category?
Thanks in advance for you help.
Shortly - replace category param with category__not_in
Example with get_posts
get_posts([
'order_by' => 'rand',
'category__not_in' => $exclude_this_category
]);
Example based on your code affects all queries, I'd not recommend to use it.
Also is_category() means you are already view single category page, better to prevent that user able to visit page with category you want to exclude. But for your example code below should work
add_action( 'pre_get_posts', 'generate_random_category_posts', 100 );
function generate_random_category_posts( $query ) {
$catto = get_queried_object();
if ( $query->is_archive() && !$query->is_category() && $query->is_main_query()) {
$query->set( 'orderby', 'rand' );
$query->set( 'category__not_in', $exclude_this_category );
}
}
More info here.
How can I hide posts from some category with some ID from main page of my site? I need solution like filter:
function exclude_post($query) {
if ($query->is_home) {
...
}
return $query;
}
add_filter('pre_get_posts', 'exclude_post');
Can somebody provide an example?
Use $query->set( $query_var, $value ); where $query_var is the variable you want to add/update in query. So put this inside your condition:
// 1st parameter is the query variable the 2nd is its value, in this case an array of category IDs
$query->set( 'category__not_in', array( 2, 6 ) );
Remember is good practice put in condition a check to $query->is_main_query(). pre_get_posts is an action hook so you have to change add_filter to add_action.
An action hook doesn't return a value, a filter does.
Example
function exclude_post( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category__not_in', array( 2, 6 ) );
}
}
add_action('pre_get_posts', 'exclude_post');
UPDATE
According to the new details emerging by the question,in order to exclude some posts in feeds stream, but not in category archive, the conditional check might look like:
if( $query->is_feed() && !$query->is_archive() )
OR
if( $query->is_feed() && !$query->is_category() )
Hope it helps!
you can also use the following way to exclude the a category from post query
<?php
if ( is_home() )
{
query_posts($query_string . '&cat=-3');
}
?>
You can use simple exclude in the query parameters :-
<?php wp_list_categories('orderby=name&show_count=1&exclude=10'); ?>
Its only sample , how to you exclude.
Hop it work..
Is it possible to change the Wordpress search permalink from:
www.mydomain.com/search/my+result+keyword+term
to:
www.mydomain.com/my-result/keyword-term
and to display the search page or any ideas for this.
Thank You.
Open the searchform.php within your theme folder and change the form action. You can of course even rebuild the whole search form if you like.
The wordpress codex has a usefull tutorial on this topic:
http://codex.wordpress.org/Creating_a_Search_Page
It's too late but could help others like me :)
Add following code into your functions.php file.
function wp_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
}
}
add_action( 'template_redirect', 'wp_change_search_url' );
function wp_search_rule(){
add_rewrite_rule('^search/([^/]*)?', 'index.php?s=$matches[1]', 'top');
}
add_action( 'init', 'wp_search_rule' );
I have set my wordpress blog's frontpage to show only three posts.
On the archive.php template, when the posts of a tag are viewed, I want to show 10 results.
How do I do this?
I tried this php code. But instead of only showing the posts with a certain tag, it queries all recent posts.
//in archive.php (before the loop)
query_posts('posts_per_page=10');
Just append the query with a tag parameter (as shown here: http://codex.wordpress.org/Function_Reference/query_posts#Tag_Parameters):
query_posts('posts_per_page=10&tag=your_desired_tag');
EDIT: If you use this within a function you can also just append your limit to the original query like this:
function my_archive_loop($content) {
global $query_string;
query_posts($query_string . "&posts_per_page=10");
}
The variable $query_string should than include all the default parameters like the current tag, category, year or whatever archive page you are viewing.
can you try this:
function post_per_page_control( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
// For archive.You can omit this
if ( is_archive() ) {
//control the numbers of post displayed/listed (eg here 10)
$query->set( 'posts_per_page', 10 );
return;
}
// For your tag
if ( is_tag() ) {
//control the numbers of post displayed/listed (eg here 10)
$query->set( 'posts_per_page', 10 );
return;
}
}
add_action( 'pre_get_posts', 'post_per_page_control' );
read more here:
1 http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
2 http://codex.wordpress.org/Function_Reference/query_posts