Change default order of pages in WordPress Admin / Backend - php

I am trying to change the default sorting order of the pages in my WordPress backend. I know this can easily be done by clicking on the tab "Title", "Date" or "ID" but those are merely one-time settings and I need a global = default solution.
I went ahead and tried using this function which to me makes perfect sense but it just doesn't work with WordPress 4.2.3 :-(
function set_post_order_in_admin( $wp_query ) {
global $pagenow;
if ( is_admin() && 'edit.php' == $pagenow && !isset($_GET['orderby'])) {
$wp_query->set( 'orderby', 'title' );
$wp_query->set( 'order', 'asc' );
}
}
add_filter('pre_get_posts', 'set_post_order_in_admin', 5 );
Any idea why this is not working any more? How can I achieve that?
Thanks + regards,
Henning

Just change order "ASC" to "DESC" in your own code, it will work perfectly. Or copy and paste below mentioned code into your functions.php :
function set_post_order_in_admin( $wp_query ) {
global $pagenow;
if ( is_admin() && 'edit.php' == $pagenow && !isset($_GET['orderby'])) {
$wp_query->set( 'orderby', 'title' );
$wp_query->set( 'order', 'DESC' );
}
}
add_filter('pre_get_posts', 'set_post_order_in_admin', 5 );

Use this snippet of code :
function set_post_order_in_admin( $wp_query ) {
global $pagenow;
if ( is_admin() && 'edit.php' == $pagenow && !isset($_GET['orderby'])) {
$wp_query->set( 'orderby', 'title' );
$wp_query->set( 'order', 'DSC' );
}
}
add_filter('pre_get_posts', 'set_post_order_in_admin' );

Related

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

Hide all pages except one from specific user in WordPress backend

I'm trying to hide all pages except one specific from an user (ID=14). This is what I got so far with the help of this post »https://www.johnparris.com/how-to-hide-pages-in-the-wordpress-admin/«:
function jp_exclude_pages_from_user($query) {
if ( ! is_admin() )
return $query;
global $pagenow, $post_type;
if ( !$current_user->14 && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__in'] = array( '10' ); // Enter your page IDs here
}
add_filter( 'parse_query', 'jp_exclude_pages_from_user' );
Result is that my site stops working.
There's an issue with your code. You are fetching user id outside the function. In that case the code should be as below:
function jp_exclude_pages_from_admin($query) {
if ( ! is_admin() )
return $query;
global $pagenow, $post_type;
$current_user_id = get_current_user_id();
if ( $current_user_id == 'youruserid' && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__not_in'] = array( 'yourpageid' ); // Enter your page IDs here
}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );
In the above section you forgot to put $current_user_id that's why it got suspended. Hopefully this code will work. Let me know if it works or not. I've tested it out & it works perfectly on my end

Strange side effect of a PHP post query code in Wordpress

Why this code in functions.php has this strange side effect of switching the menu to the mobile version in not-home pages in wordpress?
function my_blog_category( $query ) {
if ( $query->is_home() && !is_front_page() || is_archive()) {
$query->set( 'cat', '6');
}
}
add_action( 'pre_get_posts', 'my_blog_category' );
This code should affect only posts in blog and archive page so why's that?
The reason it's not working is because you were asking:
If $query->is_home() && !is_front_page()
Or, is_archive()
Group your conditional expression using brackets.
function my_blog_category( $query ) {
if ( $query->is_home() && ( !is_front_page() || is_archive() ) ) {
$query->set( 'cat', '6');
}
}
add_action( 'pre_get_posts', 'my_blog_category' );
Now you're asking:
If $query->is_home()
And, if !is_front_page() or is_archive()

How do I delete the old Custom Post Type permalink after rewriting the URL to exclude the slug?

I have a Custom Post Type called Book, and the link is: mywebsite.com/book/mybookname
I want to change this so that the link is mywebsite.com/mybookname.
I have added the following code to change the link and it works as expected:
function books_theme_remove_slug( $post_link, $post, $leavename ) {
if ( 'book' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'books_theme_remove_slug', 10, 3 );
function books_theme_parse_request( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'book', 'page' ) );
}
}
add_action( 'pre_get_posts', 'books_theme_parse_request' );
The problem is that the old link(mywebsite.com/book/mybookname) is still working. I'd like to make that link go to a 404 page without breaking the current links.
I tried the following but it breaks everything:
function books_theme_parse_request( $query ) {
if(isset($query->query['post_type']) && $query->query['post_type'] == 'book'){
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 ); exit();
}
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'book', 'page' ) );
}
}
add_action( 'pre_get_posts', 'books_theme_parse_request' );
How do I delete old url?
This shouldn't be happening in the first place, so you shouldn't try to fix it programmatically - instead you should fix it at source. Try to identify the cause and fix it. Otherwise you could introduce other issues down the line.
Some possible solutions, depending on the cause:
Flush your Rewrite Cache
Wordpress doesn't write redirections into the .htaccess, it uses rewrite rules to parse the url and find a match for the redirection.
It means that if you don't refresh your rewrite rules, the old links still work.
Ref: SarahCoding's answer to 'Remove Old Permalinks?'
How to do it: Re-saving your permalinks will flush the rewrite rules, but there if that doesn't work there are Three Ways to Flush the Rewrite Cache in WordPress
Clear your Cache
If you have an Caching plugins installed, they will need to be cleared. Some security plugins also use caching e.g. Securi. It could also just be cached in your browser.
How to do it: See How to Clear Your Cache in WordPress
Delete old WP permalinks
When you update a slug, the old permalinks are still stored in the database. This could cause issues if you want to use a slug that you had previously used for example.
How to do it: The old permalinks are stored in the table postmeta with the meta_key of _wp_old_slug. To clear all of the old slugs, run this query in your WP database:
DELETE FROM wp_postmeta WHERE meta_key = '_wp_old_slug';
Ref Mark Dave Tumanda's answer to 'Remove Old Permalinks?'
Check Redirection Plugins
If you are using any redirection plugins, check the redirection rules in case there is anything there that is clashing with your new urls.
Based on your comment all you need then is to know if the url contains /book ... see below for the added snip-it:
function books_theme_parse_request( $query ) {
global $wp;
$current_url = home_url( $wp->request );
if (strpos($current_url, "/book" == false)) { return; }
if(isset($query->query['post_type']) && $query->query['post_type'] == 'book'){
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 ); exit();
}
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'book', 'page' ) );
}
}
add_action( 'pre_get_posts', 'books_theme_parse_request' );
As people have mentioned reference
A Warning About Admin Usage
This filter can also be used to affect admin screen queries. Be sure to check if your modification is affecting your post edit screens. For example, just checking is_main_query() and is_post_type_archive('movie') will also change the query for the edit.php?post_type=movie admin screen, unless you also check for !is_admin()

Add categories to default WordPress search

I have a search form containing select fields. The first two are populated with custom taxonomies and the third with the default wordpress categories. When using the first two only for a query it works fine. When I use the third( the categories), the search query just ignores the field and comes up with the same results. How can I fix this?
I've used these functions to make them work:
function ftiaxnospiti_filter_search($query) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_search ) {
$query->set( 'post_type', array('post', 'seller') );
}
return $query;
};
add_action('pre_get_posts', 'ftiaxnospiti_filter_search');
function ftiaxnospiti_add_custom_types_to_tax( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// Get all your post types
$post_types = array( 'post', 'seller' );
$query->set( 'post_type', $post_types );
}
return $query;
}
add_action( 'pre_get_posts', 'ftiaxnospiti_add_custom_types_to_tax' );

Categories