1

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?

9
  • 2
    Your snippets, apart from the 3rd one (which contains a typo - '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 your foreach 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 the get_type_data() method?
    – Sally CJ
    May 20, 2022 at 0:39
  • 1
    The first code example should be executing subtractive iterations of hooked callbacks - the first hooked callback should execute n times (as many times as there are loop iterations), the second hooked callback executing n-1 times, etc. The second code example is an invalid add_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 callback die()s or is throwing a fatal PHP error - we need more context to answer the question.
    – bosco
    May 20, 2022 at 6:40
  • 1
    "so I am running that via a cron job to return" - how did you setup the cron job? Are you using WP-Cron - if so, what's your code and why do you need to manually call do_action()? 🤔
    – Sally CJ
    May 20, 2022 at 22:25
  • 1
    Sorry, I wasn't notified of your last reply.. :/ anyway, what I had in mind was that you hadn't actually setup any cron jobs and that you're just trying to get the loop working through the normal flow, i.e. not via a cron event's action. So if that's correct, then it's strange that get_type_data() only got called for the 1st item, if the categories array ($type in the question) actually contained two or more items.. 🤔
    – Sally CJ
    May 24, 2022 at 1:40
  • 1
    Hi @SallyCJ, thank you. I have done the cron job as a single cronjob call, that gets all the types first, and then loops to get the data per type in the same call. This works now. I am at a loss to figure out what I did wrong before, but it is working for now. Thanks for your time! May 26, 2022 at 5:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.