I have a setup similar to this:
function get_type_data($val) {
// Get the data from the DB where the ID is $val and return it.
}
add_action('my_hook', array($this, 'get_type_data'), 10, 1);
I need to repeat this callback for several results of another callback, which I was thinking of doing like this:
// Get the types first, then...
foreach ($type as $val) {
add_action('my_hook', array($this, 'get_type_data'), 10, 1);
do_action('my_hook', $val);
}
But it only does the first one. I then did it as follows:
// Get the types first, then...
foreach ($type as $val) {
add_action('my_hook_' . $val, array($this, 'get_type_data'), 10, 1);
do_action('my_hook_' . $val, $val);
}
I figured that this would then create sort of a dynamic hook and add the same action to it, but use the new parameter. Again, it only does the first one.
Then, just as a test, I did it like this:
add_action('my_hook_1', array($this, 'get_type_data'), 10, 1);
do_action('my_hook_1', 1);
add_action('my_hook_2', array($this, 'get_type_data'), 10, 1);
do_action('my_hook_2', 2);
add_action('my_hook_3', array($this, 'get_type_data'), 10, 1);
do_action('my_hook_3', 3);
Again, it only did the first one.
Finally, I did it like this, to see if I can call the same hook with different parameters:
add_action('my_hook', array($this, 'get_type_data'), 10, 1);
do_action('my_hook', 1);
add_action('my_hook', array($this, 'get_type_data'), 10, 1);
do_action('my_hook', 2);
add_action('my_hook', array($this, 'get_type_data'), 10, 1);
do_action('my_hook', 3);
I also tried with different priority values as the third parameter to add_action. Nothing works for me.
I failed to find resources indicating how this can be done. Even if it worked, the last two can't be a solution, as the data is dynamic from a web service.
Edit:
As requested by @Sally CJ, a bit more info:
- I am trying to run that hook several times for different values.
- I have, let's say two categories (but could be more) that need to run to populate a local database with data from a REST service. I first make a call to the REST service to get the categories back. Then for each of the categories returned, I need to call
get_type_data
with each category ID and pull all the data from the REST service for that category, and add it to a local database. - The result set is too large to let multiple users run the rest service on demand, so I am running that via a cron job to return and prep the data (it only changes every hour or so), and then each user can get their subsection of the data on demand from a local database.
Let's say my categories returned are 1 and 2. When doing any of the loops, only data with category_id
1 are returned, similar to:
// Result from call 1
[
[
'category_id' => 1,
'name' => 'Sample 1'
],
[
'category_id' => 1,
'name' => 'Sample 2'
]
]
// Result from call 2 never happening.
Where I was expecting results like this:
// Result from call 1
[
[
'category_id' => 1,
'name' => 'Sample 1'
],
[
'category_id' => 1,
'name' => 'Sample 2'
]
]
// Result from call 2.
[
[
'category_id' => 2,
'name' => 'Sample 1'
],
[
'category_id' => 2,
'name' => 'Sample 2'
]
]
I can confirm the data is available when I swop the statements around (so the second one brings data, and the first one then does not).
Any ideas?
'my_hook_' , $val
should be'my_hook_' . $val
), looked good to me, so, "it only does the first one" - what exactly do you mean by that - can you show a sample$type
array/values you used and the output of yourforeach
loop (which basically calls$this->get_type_data( $val )
)? And what are you really trying to do, is it to pass the dynamic$val
value to theget_type_data()
method?n
times (as many times as there are loop iterations), the second hooked callback executingn-1
times, etc. The second code example is an invalidadd_action()
syntax which should be throwing errors, short of some very unusual edge cases. None of the examples should only be executing just the first hooked callback unless that callbackdie()
s or is throwing a fatal PHP error - we need more context to answer the question.do_action()
? 🤔get_type_data()
only got called for the 1st item, if the categories array ($type
in the question) actually contained two or more items.. 🤔