How to hide a specific page instead all pages from search - php

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.

Related

Wordpress hide current blog post from related posts

I have my blog posts set up so that it shows a blog roll of other posts at the bottom. I am trying to hide the current post that the user is on from the other posts as it would not make sense to appear there.
The problem I am having is that it is hiding the first blog post from the blog landing page also. I am using the following code in my functions.php file:
add_action( 'pre_get_posts', function ( $query ) {
// not an admin page and not the main query
if ( ! is_admin() && ! $query->is_main_query() ) {
$query->set( 'post__not_in', array( get_the_ID() ) );
}
} );

WordPress Pagination problem for one category - pagination fix

somebody tried to fix pagination problem on my site by adding a function to your functions.php file[![screenshot - console][1]][1]
unfortunately, after updating wordpress or acf, this function does not work and when you try to go to the next page in the "atom" category, it displays 404 - Sorry, this page does not exist.
The pagination problem concerns only one category (subcategory). In the functions.php file I found a function like this:
function fix_atom_category_paged_query( $q ) {
if ( ! is_admin() && is_category( 'atom' ) && $_GET['debug'] == 1 ) {
$q->set( 'post_type', 'post' );
$q->set( 'posts_per_page', 9 );
// wp_die( var_dump( $q ) );
return $q;
}
}
add_action( 'pre_get_posts', 'fix_atom_category_paged_query', 2, 1 ); ```
[1]: https://i.stack.imgur.com/rKqzT.png
I suspect that the problem with the category called "atom" is due to the canonical link building and the wordpress core build itself. Names such as rss, feed, rss2, rdf, atom may conflict.
Ok, I tested it on another site. If category is called "atom", then wordpress pagination does not work for the archive of this category. I think this is WordPress problem.

Change WordPress search to produce WooCommerce search results only

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

/page/2/ doesn't work on category page - Wordpress

I'm busy with this for two days now. I have the following setup in Wordpress:
Permalinks:
/%category%/%postname%/
Files (among others):
category-blog
category-podcast
category-ebooks
Created categories:
blog (standard)
podcast
ebooks
The links site.com/blog, site.com/podcast and site.com/ebooks work, but when I go to site.com/blog/page2/ the page can't be found. Same problem with the other category pages.
I can see the posts in the category, but can't access page 2 etc.
When I create a new post type it works, but not for the standard Wordpress posts. Also, I don't want to create a new post type but want to use categories.
What can I do?
You can try below code.
<?php
function paginated_category( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_category() ) {
$query->set( 'posts_per_page', 2 );
}
}
}
add_action( 'pre_get_posts', 'paginated_category' );
?>
I found this solution (after days of search) which seems to work:
Fix 404 errors

How to Show random post on WP, except the last post

I was able to locate part of the answer, I'm new to Wordpress.
Im looking to show random post on the main page, but except the last post
i found this
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts($query) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set('orderby', 'rand');
}
}
Thank you in advance
Here is the code please use this code in functions.php file and enter the last post id which you need to exclude the post.add_action('pre_get_posts','my_pre_get_posts');
function my_pre_get_posts($query) {if ( $query->is_home() && $query->is_main_query() ) { $query->set('orderby', 'rand'); $query->set('post__not_in',array(1));}}Please use it and I 'm also searching a best possible solution in which you need not to enter the static last post id =1 .
Thanking you.

Categories