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

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.

Related

How to hide a specific page instead all pages from search

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.

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

wordpress custom is_front_page logic

I would like to apply my own logic in WordPress that'll determine if the current page is the front page or not and then I want is_front_page() to return that result. Is there anyway to hook into that function or use the 'pre_get_posts' action to change the necessary values so that is_front_page() is affected? is_front_page() needs to use my custom logic because I'm using other 3rd party plugins that uses that function and I don't want to modify those plugins so that when I update them in the future, my site won't break. Has someone needed to do this before and/or could show me what variables of the WP_Query object I need to change? Thanks!
There is no way to "hook" into is_front_page(), you're going to have to create your own function and use that instead.
The function itself is very simple, I've included it for you below - use this as a starting point for something new and put it in your functions.php file.
public function is_front_page() {
// most likely case
if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
return true;
elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
return true;
else
return false;
}

Wordpress, meta_filter_posts damaged the custom-menu widgets

when i add this code to my functions.php
function meta_filter_posts( $query )
{
if(is_tag() && is_main_query())
{
$currentTagId = get_queried_object()->term_id;
$query->set('orderby','meta_value_num');
$query->set('meta_key', 'rank_tag_'.$currentTagId.'');
$query->set('order', 'ASC');
}
}
add_filter( 'pre_get_posts', 'meta_filter_posts' );
my sidebar custom menu-widgets doesnt work anymore. The widgets show only the widget-title but not the widget-content.
The rest, e.g. the text-widgets are working normal.
But why? What is wrong with my code?
You have two problems here:
When using pre_get_posts, you should always make sure that you target the front end only. pre_get_posts alters all type of queries front end and backend
is_tag() and is_main_query() should be member variables of $query
You can do something like this
if(!is_admin() && $query->is_tag() && $query->is_main_query())

Wordpress, filter messages on main page

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..

Categories