Adding random to Wordpress Query - php

I have an existing Wordpress page and use for a page to display post the pre_get_post funtions below. The Query filters in the website all posts for the custom post types "branchenbuch" and orders it DESC for a field profiltyp (People with a logo and people without a logo) This way all records are displayed in this sepcific order (people with a logo first). Now within these results (people with a logo) I also want the results to be displayed randomly, that not always the same post comes up first.
Currently this is in my funtions.php:
add_action( 'pre_get_posts', 'custom_post_type_archive' );
function custom_post_type_archive( $query ) {
if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'branchenbuch' ) ) {
$query->set( 'meta_key', 'profiltyp' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'DESC' );
}
}
I have no idea how to do this? Thanks for your help

I'm not sure I understand your question...but you can use 'orderby' => 'rand' to randomly order query results in WordPress:
if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'branchenbuch' ) ) {
$query->set( 'meta_key', 'profiltyp' );
$query->set( 'orderby', 'rand' );
}
Read more about orderby options in the Codex.

Use the code
get all the selected posts which you need to be shown in the Top in an array let us say $allowed_posts = array(121,233,144,900);
$query->set( 'post__in', $allowed_posts );
$query->set( 'meta_key', 'profiltyp' );
$query->set( 'orderby', 'rand' );

Related

Exclude category from search results in WordPress

I am trying to exclude a specific category ID from showing in WordPress search results. I have tried with this code which I have found several places on the internet, but it does not seem to work. I have changed the ID to the correct ID.
function my_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
$query->set( 'cat','-21' );
return $query;
}
add_filter( 'pre_get_posts', 'my_search_filter' );
Right now my solution is using the code below, where I have to manually insert every page ID. It is working, but not a good solution.
add_action('pre_get_posts','exclude_posts_from_search');
function exclude_posts_from_search( $query ){
if( $query->is_main_query() && is_search() ){
//Exclude posts by ID
$post_ids = array(52384,52366,52058,52392,52374);
$query->set('post__not_in', $post_ids);
}
}
Would it be possible to use my current code with categories instead of every page ID? Or is the first code example the way to go, but with some changes?
Place the following function in your active theme functions.php file
function exclude_categories_from_search($query) {
// run query only if we are searching
if ( !$query->is_search )
return $query;
$term_ids = array( 19, 18, 214, 226, 20 ); add category/term ids
$taxquery = array(
array(
'taxonomy' => 'category', //Here add your taxonomy eg: category
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'NOT IN'
)
);
$query->set( 'tax_query', $taxquery );
}
add_action( 'pre_get_posts', 'exclude_categories_from_search' );

Multiple orderby arguments on WooCommerce archive page product query

I want to order products on archive pages by stock status (outofstock at the end of list) and price (lowest first).
For now menu order set to default and this code is used:
add_action( 'woocommerce_product_query', 'sort_by_stock_status', 999 );
function sort_by_stock_status( $query ) {
if ( is_admin() ) return;
$query->set( 'meta_key', '_stock_status' );
$query->set( 'orderby', array( 'meta_value' => 'ASC' ) );
}
And this gives me ability to show products ordered by stock status.
I was trying to edit code so it will order by stock AND price...no luck
Here is what i have tried:
add_action( 'woocommerce_product_query', 'sort_by_stock_status_and_menu_order', 999 );
function sort_by_stock_status_and_menu_order( $query ) {
if ( is_admin() ) return;
$query->set( 'meta_key', '_stock_status' );
$query->set( 'orderby', array( 'meta_value' => 'ASC', 'menu_order' => 'ASC' ) );
}
If i set menu to "order by price" i see products order only by price instock and outofstock together...
Could someone please help me with this? Maybe it`s already achieved in some of your websites...))
YOU were very near… You need to make some changes in your code get multiple "orderby" arguments using one string only instead of an array (each argument is separated by a space in this unique string):
add_action( 'woocommerce_product_query', 'sort_by_stock_status_and_menu_order', 999 );
function sort_by_stock_status_and_menu_order( $query ) {
if ( is_admin() ) return;
$query->set( 'meta_key', '_stock_status' );
$query->set( 'orderby', 'meta_value menu_order' );
// $query->set( 'order', 'ASC' ); // <== 'order' argument is already "ASC" by default
}
The "order" query argument is already ASC by default, so there is no need to change it.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Weird Order of Wordpress Category Post

Does anyone have ever seen this?
When we're accessing one of wordpress category post here:
On the top item, it's not the latest posted article instead it's the oldest one.
Which part of the php code should be changed ? It's for wordpress version of 4.5 from this link.
function change_category_order( $query ) {
$category = get_queried_object();
$cat_id=$category->term_id;
if ( $query->is_category($cat_id) && $query->is_main_query() ) {
$query->set( 'order', 'DESC' );
}
}
add_action( 'pre_get_posts', 'change_category_order' );
If you use any custom query add this also in post loop 'order' => 'DESC'
$args = array(
'post_type' => 'post',
'order' => 'DESC', );
$q = new WP_Query($args);
or paste this in function.php
add_action( 'pre_get_posts', 'my_change_sort_order');
function my_change_sort_order($query){
if(is_archive()):
//If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type )
//Set the order ASC or DESC
$query->set( 'order', 'DESC' );
//Set the orderby
//$query->set( 'orderby', 'title' );
endif;
};

Adding a custom post type from a specific term in a taxonomy to the main loop (Wordpress)

I have a custom post type "event" and a custom taxonomy "event-category". In that taxonomy I have a term "news". What i'd like is to add only the events with the term "news" to my main loop.
I have this code that will merge my events and posts but I don't know how to limit that to a specific term... Any ideas?
function add_custom_post_type_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'event') );
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
Update your function to add the tax_query query var :
function add_custom_post_type_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'post_type', array('post', 'event') );
$query->set( 'tax_query', array(array(
'taxonomy' => 'event-category',
'terms' => 'news'
)));
}
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

How to Show random post on WordPress, except the last published post

I have a website where people can upload articles, but I would like to show a random post, except the last published post on the main page, because if all the post are random, the user can not see his post when recently uploaded.
I was able to locate this, this is the one that I'm currently using
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');
}
}
This is totally untested, but you can use get_posts to gt the latest post ID and then exclude it from the main query
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts($query) {
if ( $query->is_home() && $query->is_main_query() ) {
$newest = get_posts( array( 'posts_per_page' => 1, 'fields' => 'ids' ) );
$query->set( 'post__not_in', $newest );
$query->set('orderby', 'rand');
}
}

Categories