• Hello,
    I need help for this feature.

    If I add post meta with a static value, it works:

    add_action( "save_post", 'action_save_post_regular');
    			if(!function_exists('action_save_post_regular')) {
    			// define the save_post_to_book callback
    				function action_save_post_regular( $post_id ) {
    					add_post_meta( $post_id, '_enable_role', 1, true );
    				};
    			}

    If I need to pass a variable, I don’t know how can I do. I tied something like this, but It doesn’t work:

    $variable = 'hello world';
    add_action( "save_post", 'action_save_post_regular');
    			if(!function_exists('action_save_post_regular')) {
    			// define the save_post_to_book callback
    				function action_save_post_regular( $post_id, $variable ) {
    					add_post_meta( $post_id, '_enable_role', $variable, true );
    				};
    			}

    Can anyone help me?
    thank you.
    Bye

Viewing 5 replies - 1 through 5 (of 5 total)
  • $variable is unknown in your function. It is not set as parameter on this hook.

    save_post has 3 parameters and you can’t influence them: https://developer.www.ads-software.com/reference/hooks/save_post/

    The question for me is: what do you actually want to achieve here? I guess you want to add a value to a post-meta-field every time a post is saved (apparently no matter what type). But what kind of value is it?

    Thread Starter guardiano78

    (@guardiano78)

    Hello,
    I don’t want to run it every time, but only when a specific if condition is true.
    And yes, I just need to add a custom meta post while saving the post.
    Do you think this is not the right way to do it?
    Maybe I must use do_action? … or what?
    And how I can pass my custom variable?

    thank you.

    • This reply was modified 2 years, 5 months ago by guardiano78.

    Then I would recommend the following:

    add_action( "save_post", 'action_save_post_regular', 10, 3);
    if(!function_exists('action_save_post_regular')) {
     // define the save_post_to_book callback
     function action_save_post_regular( $post_ID, $post, $update ) {
      $varibale = 1;
      add_post_meta( $post_id, '_enable_role', $variable, true );
     };
    }

    Unfortunately I still don’t know where the value for this variable you write about should come from. In my example above I just set it inside the function. It is not possible to add a parameter in the hook. If you describe more exactly where the value comes from, you could possibly find a solution.

    You would have to add the condition you mentioned here under which this should be executed. If you want to use this only on a specific post type, I would recommend this action hook instead:

    add_action( "save_post_myposttype", 'action_save_post_regular', 10, 3);

    you have to replace myposttype with the post type name you want to use.

    Thread Starter guardiano78

    (@guardiano78)

    Hello,
    my post is a Woocommerce product.
    The variable is created before the function and I have to pass it inside the function.
    my custom meta post is called “_role_based_price”. The meta post must contain a serialized array of a role/price combination.
    That’s all.

    “Before the function”? So at each call of each page? Makes little sense to me to do something like this globally.

    You write that it goes in the WooCommerce products. Is the required data possibly generatable at the product or about it? Then you can get the product with its data within the function via $post->ID and wc_get_product().

    If the data is really globally available, you can also include the variable as a global variant in the function:

    function action_save_post_regular() {
     global $variable;
     // ...
    }

    Makes little sense to me only as said.

    Seems to me you should think again about your concept here. The values you want to store on the way, you should only compose and set within the function.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘add_action with add_post_meta’ is closed to new replies.