Order post by custom fields - php

In advance many thanks for read this question!
I have order my post by a custom field (eslora), but when apply the filter (category) this sort is broken.
Some one know how can order my post on this way by default.
Best regards!
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post' ) {
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'eslora');
$query->set('order', 'ASC');
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');

Related

WooCommerce admin sort orders by order status

I'm trying to add custom code to enable admin orders table sorting by order status. The sorting works, but it looks like it doesnt sort properly by asc / desc. I have written this code, but for some reason the sorting is random, it doesn't sort by asc and desc..
// Make custom column sortable
add_filter( "manage_edit-shop_order_sortable_columns", 'shop_order_column_meta_field_sortable' );
function shop_order_column_meta_field_sortable( $columns )
{
$meta_key = 'order_status';
return wp_parse_args( array('order_status' => $meta_key), $columns );
}
// Make sorting work properly (by numerical values)
add_action('pre_get_posts', 'shop_order_column_meta_field_sortable_orderby' );
function shop_order_column_meta_field_sortable_orderby( $query ) {
global $pagenow;
if ( 'edit.php' === $pagenow && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type'] ){
$orderby = $query->get( 'orderby');
$meta_key = '_order_status';
if ('_order_status' === $orderby){
$query->set('meta_key', $meta_key);
$query->set('orderby', 'meta_value_num');
}
}
}

Change default sorting in Woocommerce order list

How can the default sorting of the Woocommerce order list be changed. For now each time I enter the order page, orders are displayed as DESC. I would like it to be ASC by default each time I enter the order page. Can anyone help?
As I found another custom order solution in the forum:
sort-orders-by-paid-date-in-woocommerce-admin-order-list
I edited the code mentioned in there to the following, which worked out:
function action_parse_query( $query ) {
global $pagenow;
// Initialize
$query_vars = &$query->query_vars;
// Only on WooCommerce admin order list
if ( is_admin() && $query->is_main_query() && $pagenow == 'edit.php' && $query_vars['post_type'] == 'shop_order' ) {
// Set
$query->set( 'orderby', 'date' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'parse_query', 'action_parse_query', 10, 1 );
These code will do the work.
add_filter( 'woocommerce_order_query_args', function ( $args ) {
$args['order'] = 'ASC';
return $args;
} );

Advanced Custom Field Filter Wordpress

Could someone please explain why the first section of code works but not the second. I am trying to filter the content on a website based on what values have been selected in the backend on each post using Advanced Custom Fields.
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'property' ) {
// allow the url to alter the query
if( isset($_GET['bedrooms']) ) {
$query->set('meta_key', 'bedrooms');
$query->set('meta_value', $_GET['bedrooms']);
}
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
non working:
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ){
return;
}
// get meta query
$meta_query = $query->get('meta_query');
if( isset($_GET['bedrooms']) ){
$meta_query[] = array(
'key' => 'bedrooms',
'value' => $_GET['bedrooms'],
'compare' => '=',
);
}
// update meta query
$query->set('meta_query', $meta_query);
return;
}

Modifying search function to exclude posts and limit results to 10 per page on front end only

This problem is kind of two part. I have the solution to exclude posts from the search and limit the results to 10 results per page but the problem is it also affects the backend wp-admin area. So if i search for posts through the wp-admin area the posts will be excluded even though I only want this to occur for users who are not admin. This is the code I have for excluding posts and limiting the results to 10:
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
function change_wp_search_size($query) {
if ( $query->is_search ) // Make sure it is a search page
$query->query_vars['posts_per_page'] = 10;
return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');
Now I have done a little bit of reading and research on stack and other sites and it looks like there is a way to only trigger these functions only after checking if admin is logged in. I'll reference to the wordpress codex for the is_admin function
This is the way I attempted to use the is_admin function which caused an http error 500. Please excuse me if the code is grossly incorrect:
if ( !is_admin() ) {
function SearchFilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
if ( !is_admin() ) {
function change_wp_search_size($query) {
if ( $query->is_search && !is_admin() )
$query->query_vars['posts_per_page'] = 10;
return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');
I'm hoping that I'm close and just need some tweaking for the code
Edit:
So now this is what i have for both functions. I know its my php code which is the problem can you help further please:
function SearchFilter($query) {
if ( is_admin () ) {
return $query;
}
else {
($query->is_search) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
function change_wp_search_size($query) {
if ( is_admin () ) {
return $query;
}
if ( $query->is_search ) // Make sure it is a search page
$query->query_vars['posts_per_page'] = 10;
return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');
EDIT 2: Pastebin of the entire function.php file http://pastebin.com/tTEnHGp2
function SearchFilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
function change_wp_search_size($query) {
if ( $query->is_search && !is_admin() ) {
$query->query_vars['posts_per_page'] = 10;
}
return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');
This Should do it
Okay don't wrap your search functions around the is_admin() instead put the is_admin() inside the function
Like this:
Function search(){
if ( is_admin()){
//Search query for admin
}
else {
//Search query for users
}
}
That should do it

Wordpress $_GET parameters (from the URL) using two values of same custom field

I am attempting to return both the properties with the status of both 'sold' and 'leased'. I have successfully been able to return either the 'sold' or 'leased' properties, but not yet both.
I have added the following to my functions file:
// FILTER PROPERTIES BY STATUS //
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'properties' ) {
// allow the url to alter the query
if( isset($_GET['status']) ) {
$query->set('meta_key', 'status');
$query->set('meta_value', $_GET['status']);
}
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
The url format I am using to successfully return either the 'sold' or 'leased' properties is:
http://www.example.com/properties/?status=leased
or
http://www.example.com/properties/?status=sold
Is there a simple URL format I can use to include both values? I have tried for example:
http://www.example.com/properties/?status=leased&status=sold
Or is there a change to the function I need to make first?

Categories