最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - Count when a post of a custom post type is deleted and store it in a theme option

programmeradmin4浏览0评论

I work on a website that shows products in a catalogue without checkout. Those products can be bougth stationary, and then the product will be deleted from a custom post type. So, I basically want to count all deleted posts of a certain post type, add them to a theme option value, and then update the theme option value with the new value. I thought of something like this:

function getSoldProductsCount() {

    $addCount                  = 0;
    $oldCount                  = get_option( 'sold_products_count', 'number' );
    $newCount                  = $oldCount + $addCount;

    /*
    * Here I want to do something like: for each deleted post of posttype 'product', increase the counter value by 1
    */

    return '<span class="sold-products-count">'. $newCount .'</span>';
    update_option( 'sold_products_count', $newCount );
}

add_shortcode( 'sold_products_count', __NAMESPACE__ . '\\getSoldProductsCount' );

Now I am stuck here, because I don't know which hook I should use to increase my counter. Ist there a possibility to count all delete actions of a certain custom post type? Sorry if the question is silly, I am very new to Wordpress.

I work on a website that shows products in a catalogue without checkout. Those products can be bougth stationary, and then the product will be deleted from a custom post type. So, I basically want to count all deleted posts of a certain post type, add them to a theme option value, and then update the theme option value with the new value. I thought of something like this:

function getSoldProductsCount() {

    $addCount                  = 0;
    $oldCount                  = get_option( 'sold_products_count', 'number' );
    $newCount                  = $oldCount + $addCount;

    /*
    * Here I want to do something like: for each deleted post of posttype 'product', increase the counter value by 1
    */

    return '<span class="sold-products-count">'. $newCount .'</span>';
    update_option( 'sold_products_count', $newCount );
}

add_shortcode( 'sold_products_count', __NAMESPACE__ . '\\getSoldProductsCount' );

Now I am stuck here, because I don't know which hook I should use to increase my counter. Ist there a possibility to count all delete actions of a certain custom post type? Sorry if the question is silly, I am very new to Wordpress.

Share Improve this question asked Feb 20, 2022 at 20:23 what everwhat ever 11 bronze badge 1
  • 1 note that nothing after return will run, so your update_option call is never executed. return is your way of saying "the function has now finished, and this is the result I am returning". You can move ti to before the return statement, but now anytime this shortcode is used or displayed on an uncached page it will increment the value. For this reason, a shortcode is not suitable for this task – Tom J Nowell Commented Feb 20, 2022 at 21:44
Add a comment  | 

1 Answer 1

Reset to default 0

The hook played by WordPress just before a post is "definitively" deleted is https://developer.wordpress.org/reference/hooks/delete_post/

+the comment left by @tom-j-nowell.

Regards

发布评论

评论列表(0)

  1. 暂无评论