Weird Order of Wordpress Category Post - php

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

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' );

How to exclude categories from Archive.php WordPress

I created a blognews.php page (it shows the last news with a monthly archive) with this code to exclude some categories.
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__in' => 54,
'category__not_in' => 3 ,
'order' => 'DESC'
);
$query = new WP_Query($args);
and it works, the articles under this category aren't displayed.
But when I do the same thing to archive.php and I click a month, I see the posts under the excluded category.
I tried this but it doesn't work:
<?php if ( have_posts() ) : ?>
<?php
query_posts( array( 'category__and' => array(54), 'posts_per_page' => 20, 'orderby' => 'title', 'order' => 'DESC' ) );
while ($query->have_posts()):
$query->the_post();
get_template_part( 'template-parts/content', 'excerpt' );
Is there an easy way to exclude more categories from the blognews.php page and the archive? What's wrong with my code? Or show only a specific category it will be good too.
Add this code to the functions.php file
function exclude_category( $query ) {
if ( $query->is_archive() && $query->is_main_query() ) {
$query->set( 'cat', '-3' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
Here all conditions where is_archive is true:
if ( $this->is_post_type_archive
$this->is_date
$this->is_author
$this->is_category
$this->is_tag
$this->is_tax )
$this->is_archive = true;

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.

Woocommerce how to display products by product SKU

One my client want to display all woocommerce product by product SKU.
Normally i used following code for display products.
$postArg = array('post_type'=>'product',
'post_status'=>'publish',
'posts_per_page'=>-1,
'orderby'=>'data',
'order'=>'DESC',
);
$queryGetFiles = get_posts($postArg);
But now my client want to show all products by product SKU in front side.
SKU like this 1041-14, 1041-12, 1041-16 ,1041,2001,3501
all product has different sku value and display which doesn't have "-" character
Anyone know how should i do this?
Try this
$postArg = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => '_sku',
'orderby' => 'meta_value' // meta_value_num if ordered by intergers
'order' => 'DESC',
);
$queryGetFiles = get_posts($postArg);
Answered with help of this post
Try to put code in function.php
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = '_sku';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'desc';
return $args;
}
Please try this. Let me know if this works perfectly....
/**
* Adds the ability to sort products in the shop based on the SKU
* Can be combined with tips here to display the SKU on the shop page: https://www.skyverge.com/blog/add-information-to-woocommerce-shop-page/
*/
function sv_add_sku_sorting( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'sku' == $orderby_value ) {
$args['orderby'] = 'meta_value';
$args['order'] = 'asc';
// ^ lists SKUs alphabetically 0-9, a-z; change to desc for reverse alphabetical
$args['meta_key'] = '_sku';
}
return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'sv_add_sku_sorting' );
function sv_sku_sorting_orderby( $sortby ) {
$sortby['sku'] = 'Sort by SKU';
// Change text above as desired; this shows in the sorting dropdown
return $sortby;
}
add_filter( 'woocommerce_catalog_orderby', 'sv_sku_sorting_orderby' );
add_filter( 'woocommerce_default_catalog_orderby_options', 'sv_sku_sorting_orderby' );
Thank You All guys for support.
I have resolved it by my self using following.
$postArg = array('post_type'=>'product',
'post_status'=>'publish',
'posts_per_page'=>-1,
'orderby'=>'date',
'order'=>'DESC',
'meta_query' => array(
array(
'key' => '_sku',
'value' => '-',
'compare' => 'NOT LIKE'
)
),
);

sort posts based on meta key in a taxonomy archive page

I am using the below code to sort posts based on my meta key sub_seminars_0_start_date. The code works fine as it gets all the posts from portfolio post_type and then sorts them based on my meta key. However when I through a taxonomy query it displays nothing, a blank page with header and footer.
<?php
$args = array(
'post_type' => 'dt_portfolio',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'dt_portfolio_category',
'field' => 'slug',
'terms' => '',
),
),
'meta_key' => 'sub_seminars_0_start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$query = new WP_Query( $args ); ?>
How can I sort my posts that are in the taxonomy archive page ?
function change_order_for_events( $query ) {
//only show future events and events in the last 24hours
$yesterday = date('Ymd');
if ( $query->is_main_query() && (is_tax('dt_portfolio_category') || is_post_type_archive('dt_portfolio')) ) {
$query->set( 'meta_key', 'sub_seminars_0_start_date' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
//Get events after 24 hours ago
$query->set( 'meta_value', $yesterday );
$query->set( 'meta_compare', '>' );
//Get events before now
//$query->set( 'meta_value', current_time('timestamp') );
//$query->set( 'meta_compare', '<' );
}
}
add_action( 'pre_get_posts', 'change_order_for_events' );
this code in function.php will sort your posts based on a specific meta key value.
sub_seminars_0_start_date
I have created a custom taxonomy Seminar_Venues but the above code won’t work for that even after adjustin the parameters. Can you look
function pre_get_posts_hook($wp_query) {
if( is_admin() ) {
return $wp_query;
}
if ($wp_query->is_main_query() && ( is_category() || is_archive() ))
{
$wp_query->set( 'orderby', 'meta_value' );
$wp_query->set( 'meta_key', 'sub_seminars_0_start_date' );
$wp_query->set( 'order', 'ASC' );
return $wp_query;
}
}
add_filter('pre_get_posts', 'pre_get_posts_hook' );
This worked for me to sort the posts based on a custom field mate key

Categories