wordpress remove post status count from cms - wordpress

I want to remove the post status count from WordPress edit.php.
My WordPress CMS have more than 500,000 posts. The publish count is loaded every time you open the page. The following query is fired every time.
This makes my Wordpress CMS loading very slow.
SELECT post_status, COUNT( * ) AS num_posts FROM wp_posts WHERE post_type = 'post' GROUP BY post_status

By tracing the code, I've worked up the only solution that I can see.
The filter bulk_post_updated_messages is ONLY called on the edit.php screen.
The counts are not calculated (in class-wp-posts-list-table.php, get_views method) if the global variable $locked_post_status is not empty.
By gluing these two pieces of information together, I've got a solution that you can use by dropping it into your theme's functions.php file:
// Hook this filter, only called on the `edit.php` screen.
// It's not the "correct" filter, but it's the only one we can leverage
// so we're hijacking it a bit.
add_filter('bulk_post_updated_messages', 'suppress_counts', 10, 2);
// We need to let the function "pass through" the intended filter content, so accept the variable $bulk_messages
function suppress_counts($bulk_messages) {
// If the GET "post_type" is not set, then it's the "posts" type
$post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : 'post';
// List any post types you would like to KEEP counts for in this array
$exclude_post_types = array('page');
// Global in the variable so we can modify it
global $locked_post_status;
// If the post type is not in the "Exclude" list, then set the $locked variable
if ( ! in_array($post_type, $exclude_post_types)) {
$locked_post_status = TRUE;
}
// Don't forget to return this so the filtered content still displays!
return $bulk_messages;
}

i came up with this solution.
//Disable Article Counter - query runs for about 1-2 seconds
add_filter('admin_init', function () {
foreach (get_post_types() as $type) {
$cache_key = _count_posts_cache_key($type, "readable");
$counts = array_fill_keys(get_post_stati(), 1);
wp_cache_set($cache_key, (object)$counts, 'counts');
}
}, -1);
add_action('admin_head', function () {
$css = '<style>';
$css .= '.subsubsub a .count { display: none; }';
$css .= '</style>';
echo $css;
});
the post counter uses the wp-cache, the idea behind this approach is, to prefill the cache with the "correct" object containing 1's (0 would skip the status from being clickable) - at the earliest moment.
it results in all stati being displayed - with 1's and the query is not run et-all
in addition it returns a css snippet to hide the (1)

Related

auto generate post title in wordpress like ABC-123456

I want to auto-generate post title like that
Ex : ABC-123456
also i need to let (( ABC- )) fixed and random change the 06 numbers
and to dont change the post title through updating the post
First, to modify WordPress behavior the correct way, you find an appropriate hook. In this case, that would be a filter that allows changing the Post data before it is saved to the db.
The filter 'wp_insert_post_data' is exactly what you need, so you add your filter, and connect it to a function like so:
function zozson_filter_post_title(){
}
add_filter( 'wp_insert_post_data', 'zozson_filter_post_title',50,4);
'wp_insert_post_data' is the name of the filter
'zozson_filter_post_title' is the name you give to your function, to hook to it.
50 is the priority. I chose 50 to run it after most other things. Default is 10
4 is the number of variables that the filter passes to your function.
So now we will add those variables and the logic inside it, to assign these CPT sho7nat those titles on admin saving them.
function zozson_filter_post_title( $data, $postarr, $unsanitized_postarr, $update){
//Then if it is the post type sho7nat
if( $data['post_type'] !== 'sho7nat' ){
return $data;
}
//If there is already a titled saved ($update returns true always)
if( $data['post_title'] !== '' ){
return $data;
}
//Let's build our title
$post_title = ' ABC-';
//What better random number that a unique timestamp?
$random_number = strtotime('now');
//Add the random number to the post title to save. You can do these in 1 line instead of 3
$post_title.= $random_number;
//We now have a post title with ABC- fixed and a random number, tell WordPress to use it as the post title
$data['post_title'] = $post_title;
return $data;
}
add_filter( 'wp_insert_post_data', 'zozson_filter_post_title',50,4);
The title automatically assigned should be like in this example:

WordPress/WooCommerce - remove wp_count_posts call in wp-admin

There's a call to wp_count_posts() on every page in wp-admin. I would think it should only happen on the product pages. Is there a way to disable the call on all pages but products? Our site has over 100,000 products, and this call slows down wp-admin pages.
The following is the caller log from Query Monitor
wp_count_posts()
wp-includes/post.php:2859
WC_Install::is_new_install()
wp-content/plugins/woocommerce/includes/class-wc-install.php:399
WC_Admin_Notices::init()
wp-content/plugins/woocommerce/includes/admin/class-wc-admin-notices.php:58
WC_Admin->includes()
wp-content/plugins/woocommerce/includes/admin/class-wc-admin.php:62
do_action('init')
wp-includes/plugin.php:470
Could you try the following. Add following function in your theme functions.php . All credits goes to - wordpress remove post status count from cms
I have edited $post_type where we want all post types and $exclude_post_types changed to product over page post type.
add_filter('bulk_post_updated_messages', 'suppress_counts', 10, 2);
// We need to let the function "pass through" the intended filter content, so accept the variable $bulk_messages
function suppress_counts($bulk_messages) {
// If the GET "post_type" is not set, then it's the "posts" type
$post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : '';
// List any post types you would like to KEEP counts for in this array
$exclude_post_types = array('product');
// Global in the variable so we can modify it
global $locked_post_status;
// If the post type is not in the "Exclude" list, then set the $locked variable
if ( ! in_array($post_type, $exclude_post_types)) {
$locked_post_status = TRUE;
}
// Don't forget to return this so the filtered content still displays!
return $bulk_messages;
}

woocommerce_output_related_products_args hook not working as expected

I want to show a specific product with id 64391 in related products section but the following code doesn't work for that. Am I missing something?
add_filter( 'woocommerce_output_related_products_args','msrp_reconfigure_related_products_args', 20 );
public function msrp_reconfigure_related_products_args($args)
{
$args['posts_per_page'] = 5;
$args['columns'] = 5;
$args['post__in'] = array(64391);
return $args;
}
As you can find on the WC core source. The filter hook woocommerce_output_related_products_args triggers via wc-template-functions.php file and passes it's return to woocommerce_related_products function. So the $args parameter isn't the args used in db query and you can not use post__in key in it.
Instead, to add specific products to related products section, you can use woocommerce_related_products filter as below:
add_filter('woocommerce_related_products', 'add_related_products');
function add_related_products($related_product_ids)
{
// WC source code stores IDs as string in this array, so I did that too
$related_product_ids[] = '81';
return $related_product_ids;
}
Tested and it's working.
Note: This filter hook has 3 parameters: $related_posts, $product_id and $args. You can limit these new products to only display on specific single product pages, by checking $product_id

Get all product variations of a product given its ID in Woocommerce

I have a custom page where I'm trying to list every products in the store along with their variations.
Also, I'm trying to list the variations' prices sorted by the product attribute with slug 'size'
For testing, I'm trying to get the variations of a single product with the ID 381
My code yet is
$handle=new WC_Product('381');
$variations1=$handle->get_avaialable_variations();
foreach ($variations1 as $key => $value) {
echo '<option value="'.$value['variation_id'].'">'.implode('/',$value['attributes']).'-'.$value['price_html'].'</option>';
}
But the error I'm getting is
PHP Fatal error: Call to undefined method WC_Product::get_avaialable_variations()
I tried using
$handle=new WC_Product_Variable('381');
instead of
$handle=new WC_Product('381');
But the error is the same.
Any help here?
Try this code.
$handle=new WC_Product_Variable('12');
$variations1=$handle->get_children();
foreach ($variations1 as $value) {
$single_variation=new WC_Product_Variation($value);
echo '<option value="'.$value.'">'.implode(" / ", $single_variation->get_variation_attributes()).'-'.get_woocommerce_currency_symbol().$single_variation->price.'</option>';
}
Note: Use this $single_variation->get_price_html() but its outputs with html span tag which results in getting hidden in option tags.
Tested the above code and the results are as follows.
Let me know if that worked for you too.
You had a typo in your code - get_avaialable_variations
It should be get_available_variations
function get_variation_data_from_variation_id($item_id) {
$_product = new WC_Product_Variation($item_id);
$variation_data = $_product->get_variation_attributes(); // variation data in array
$variation_detail = woocommerce_get_formatted_variation($variation_data, true); // this will give all variation detail in one line
// $variation_detail = woocommerce_get_formatted_variation( $variation_data, false); // this will give all variation detail one by one
return $variation_data; // $variation_detail will return string containing variation detail which can be used to print on website
// return $variation_data; // $variation_data will return only the data which can be used to store variation data
}
I found this before on stackoverflow couldn't remember the link (please edit if find the link to the answer with this method). it shows the variation data for output. the method takes variation id as perameter.
output:
.

WordPress: Having trouble in pagination for plugin settings page

Developing a plugin in WordPress and getting well but stuck on pagination for the plugin page. Here is my code downloaded from internet ( got reference from here )
$items = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->review_media GROUP BY post_id")); // number of total rows in the database
// tested and got results as commented
print_r($items); // say this is outputs value 2
echo $rm_options['list_per_page']; // this is my option set with value 1
if($items > 0) {
$p = new pagination;
$p->items($items);
$p->limit(empty($rm_options['list_per_page']) ? 20 : $rm_options['list_per_page']); // Limit entries per page
$p->target("admin.php?page=moderate.php");
$p->currentPage($_GET[$p->paging]); // Gets and validates the current page
$p->calculate(); // Calculates what to show
$p->parameterName('paging');
$p->adjacents(1); //No. of page away from the current page
if(!isset($_GET['paging'])) {
$p->page = 1;
} else {
$p->page = $_GET['paging'];
}
//Query for limit paging
$limit = "LIMIT " . ($p->page - 1) * $p->limit . ", " . $p->limit;
} else {
echo "No Record Found";
}
When I don't group my query by post_id it is working fine but as soon as I grouped its behaving weird. It is creating a pagination links and getting blank page. I think the reason is grouping the row. But don't know how to solve this.
Here is my table screenshot
Thanks a lot for your help...
If you use WordPress WP_List_Table class, it will take care of the pagination and provide a default table experience for the user.
This tutorial covers it all, the pagination part is:
function prepare_items() {
[...]
$per_page = 5;
$current_page = $this->get_pagenum();
$total_items = count($this->example_data);
// only necessary because we have sample data
$this->found_data = array_slice($this->example_data,(($current_page-1)*$per_page),$per_page);
$this->set_pagination_args( array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page //WE have to determine how many items to show on a page
) );
$this->items = $this->found_data;
}
And here's the full plugin shown in the tutorial. And in this other plugin you can see WP_List_Table in action using database data.

Resources