Title: function change statut product
Last modified: August 15, 2018

---

# function change statut product

 *  Resolved [Lust](https://wordpress.org/support/users/inumedia/)
 * (@inumedia)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/function-change-statut-product/)
 * Hello I wish create a function in function php which is switching a specific 
   post status from pubslished to draft and then from draft to published
 * I’ve tried this:
 * function change_post_status($product_id,$status){
 * $current_month = date(‘M’);
 * if( $current_month == ‘Dec’ || $current_month == ‘Jan’ ) {
 * $postid = 789;
    wp_update_post(array( ‘ID’ => $postid, ‘post_status’ => ‘published’));
 * } else {
 * $postid = 789;
    wp_update_post(array( ‘ID’ => $postid, ‘post_status’ => ‘draft’));
 * }
    }
 * but it’s no working.
 * Have you a solution to resolve this problem ?

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Luminus Alabi](https://wordpress.org/support/users/luminus/)
 * (@luminus)
 * Automattic Happiness Engineer
 * [7 years, 9 months ago](https://wordpress.org/support/topic/function-change-statut-product/#post-10616675)
 * [@inumedia](https://wordpress.org/support/users/inumedia/),
 * Taking a quick look at this code, you’re passing a `$product_id` to the function
   which never gets used and I don’t see you actually calling the function. The 
   function isn’t going to work by itself if it isn’t called.
 * All of your quotes are also wrong. You have the fancy style quotes instead of
   the regular ones e.g. `‘ID’` instead of `'ID'`. It looks like you may have edited
   the code in Microsoft Word or something of the sort.
 * I don’t see what this has to do with WooCommerce, though, since you’re trying
   to alter the post status of a regular post.
 * If you’re not sure how to get this working for your context you may want to speak
   to a developer. If you don’t have a go-to developer, we highly recommend contacting
   one of the services on our customizations page: [https://woocommerce.com/customizations/](https://woocommerce.com/customizations/).
 *  Thread Starter [Lust](https://wordpress.org/support/users/inumedia/)
 * (@inumedia)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/function-change-statut-product/#post-10634024)
 * Hello
 * Thanks for your answer.
 * In my code it’s the good quotes, may be it’s because i’v already past there in
   your tickets support and made a copy from this.
 * My goal is to switch product as publish to unplublish by activating an option
   with a custom field. My customer have some “seasonal” product and some of them
   are not avalaible in some months per years.
 *  [Luminus Alabi](https://wordpress.org/support/users/luminus/)
 * (@luminus)
 * Automattic Happiness Engineer
 * [7 years, 9 months ago](https://wordpress.org/support/topic/function-change-statut-product/#post-10637794)
 * [@inumedia](https://wordpress.org/support/users/inumedia/),
 * It isn’t working because you’re not hooking the function to anything, i.e. you
   haven’t told it when to run.
 * I attached it to the `woocommerce_shortcode_before_products_loop` hook in the
   code snippet below and it works as expected. You can see a visual representation
   of available hooks here – [https://hooks.wpdesk.org/](https://hooks.wpdesk.org/)
 * This will cause the function to run every time the home page is loaded so it 
   probably isn’t the best way to go about it if you have a popular site as it will
   run for every user that hits the homepage.
 *     ```
       function change_post_status(){
   
           $current_month = date('M');
   
           if( $current_month == 'Dec' || $current_month == 'Jan' ) {
   
               $postid = 789;
               wp_update_post(array(
                   'ID' => $postid,
                   'post_status' => 'publish'
               ));
   
           } else {
   
               $postid = 789;
               wp_update_post(array(
                   'ID' => $postid,
                   'post_status' => 'draft'
               ));
   
           }
       }
       add_action( 'woocommerce_shortcode_before_products_loop', 'change_post_status', 5 );
       ```
   
 * You’re probably better off doing this with a cron job that runs at 00:01 on December
   1st to publish the product and again at 00:01 on February 1 to set it to draft.
 *  Plugin Support [John Coy a11n](https://wordpress.org/support/users/johndcoy/)
 * (@johndcoy)
 * Automattic Happiness Engineer
 * [7 years, 9 months ago](https://wordpress.org/support/topic/function-change-statut-product/#post-10664434)
 * Hi [@inumedia](https://wordpress.org/support/users/inumedia/)
 * We haven’t heard back from you in a while so I am going to mark this thread as
   resolved. Feel free to open a new thread if you have any questions.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘function change statut product’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 4 replies
 * 3 participants
 * Last reply from: [John Coy a11n](https://wordpress.org/support/users/johndcoy/)
 * Last activity: [7 years, 9 months ago](https://wordpress.org/support/topic/function-change-statut-product/#post-10664434)
 * Status: resolved