Set default admin order for shop order - woocommerce

I'm looking for a way to sort, by default, shop order in descending order:
edit.php?post_type=shop_order&orderby=date&order=desc
There is a solution for ordering product using default_catalog_ordering_desc (https://stackoverflow.com/a/47629492/858782) but I can't find the equivalent for the shop order.
Thanks

woocommerce using Wordpress WP_List_Table and in order to modify the order by you can use the following function:
function set_orderby_in_admin($wp_query)
{
global $pagenow;
if (is_admin() && 'edit.php' == $pagenow && !isset($_GET['orderby']) && isset($_GET['post_type']) == 'shop_order') {
$wp_query->set('order', 'DESC'); //You can change it to ASC
}
}
add_filter('pre_get_posts', 'set_orderby_in_admin', 5);
Test it and working :)

Related

Set product visibility default value on "visible" (Catalog & Search)

When I edit a product on woocommerce I'd like to have the default settin woocommerce visibility options radio button, not "hidden" but "visible".
I tried to use add_filter to set default value on "visible" like this:
add_filter( 'woocommerce_product_visibility_options', 'set_visibility_default_visible', 10, 1);
function set_visibility_default_visible($array) {
//set_catalog_visibility('visible');
return 'visible';
}
but I miss all the other options:
Any suggest, thanks.
product visibility default
If I understand what you are asking, you wish the "Catalogue visibility" panel to be open by default when editing a product, so that you can see the form. You can do this by adding some css to admin ie
add_action('admin_head', 'show_catalog_visibility');
function show_catalog_visibility() {
echo '<style>
#catalog-visibility-select{
display: block;
}
</style>';
}
The woocommerce_product_visibility_options filter is used to customise the actual options available not to determine if the panel is visible or not.
Edit : given the actual problem is that the product visibility for new products is defaulting to 'hidden' which is non standard
WooCommerce by default sets new product catalog visibility to 'visible'. If a new product is not starting out this way, it suggests that something eg a plugin is overriding the default value. Obviously it would always be best to identify the source of this behaviour if possible. In the worst case if the cause can't be found, the following code will set the default catalog visibility to 'visible' when a new product is created.
add_action('transition_post_status', 'jt_override_default_product_visibility', 10, 3);
function jt_override_default_product_visibility( $new_status, $old_status, $post ) {
if ( $old_status == 'new' && $new_status == 'auto-draft' && $post->post_type == 'product' ){
$_product = wc_get_product( $post->ID );
$_product->set_catalog_visibility('visible');
$_product->save();
}
}
Existing products visibility can be updated via a bulk edit from the product list page.

Wordpress Hide child posts of custom post type in wordpress admin

I created a custom post type called "Companies".
Every time a post of post type companies is created, a child post is also created.
I would like to hide the child posts of this companies post type in the backend.
Is there a way to do this?
Thanks!
I managed to do this by using the following code:
function cleanup_companies_posttype_list($query) {
global $pagenow;
if (isset($_GET['post_type']) && $_GET['post_type'] == 'companies' && $pagenow == 'edit.php') {
$query->set('post_parent',0);
}
}
add_filter('pre_get_posts', 'cleanup_companies_posttype_list');

How can I alter the WordPress query without "overriding" it?

I have a search results page that I want to limit results on i.e. posts_per_page. However if I use query_posts('posts_per_page=6') I lose the original query.
How do I alter my query without damaging the original?
You can use the pre_get_posts filter to access/alter the $query object. In functions.php:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('posts_per_page', 6);
}
}
}
add_action('pre_get_posts', 'search_filter');
Never ever use query_posts, it breaks the main query object ($wp_query) and all functionality that relies on the main query object. It also breaks page functionality. Apart from that, it is slow and reruns queries. query_posts should be on top of your most evil list together with functions like create_function(), eval() and extract().
If you need to alter the main query, always use pre_get_posts to do so. Never change the main query with a custom one, it might solve one problem, but it creates many other.
The following will work
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin()
&& $q->is_main_query()
&& $q->is_search()
) {
$q->set( 'posts_per_page', 6 );
}
});

wordpress conditional statements

I'm trying to get this logic to work in WordPress Widget Logic:
Widget displays if it's NOT the homepage OR NOT the page 86 OR NOT if it's a child of page 86.
!is_home || !is_page('86') || !is_child('86')
I've added this function to functions.php:
function is_child($parent) {
global $post;
return $post->post_parent == $parent;
}
try this:
(!is_home() && !is_page(86) && !is_child(86))
is_home() is a function. And to check the pages I suggest using a integer not a string.
And the overall ( ) is just a safety measure. Maybe you can leave them out, I'm not sure.
Not tested.

Wordpress: How can I hook only on certain pages? Can I even do that?

So I created a action add_action( 'header', 'admin_bar', 8 );, so that it loads in the header. However, I don't want it to load on certain pages, for example, post-new.php. Can I have a condition for hooks to load only certain pages?
I believe there is a $pagenow global. not sure if it's still there but you could var_dump($pagenow) on the pages you want to exclude to find the value then do something like this in your function...
function admin_bar() {
global $pagenow;
if ( 'post-new' != $pagenow || 'dashboard' != $pagenow ) {
//execute code
}
}

Resources