customization - Deleted categories still listed until additional page refresh

admin2025-06-02  1

I've tried using wp_delete_category() and wp_delete_term() to delete a category, but after the page reloads, the deleted category still shows up in the results of get_categories(). But if I reload the page a second time, the category is gone.

How can I make sure it goes away after the first reload?


I'm grabbing form data with a $_POST and handing to wp_delete_category(). This is all taking place on a plugin settings page. Here's a little snippet of my code. Suggestions welcome.

if ( isset($_POST['deleteSelectedCategories']) ){

    $cats = $_POST['catsToDelete'];
    $catsArray = explode(',', $cats);

    foreach ($catsArray as $catID) {
        // wp_delete_category( $catID );        
        wp_delete_term( $catID, 'category' );
    }
}

I've tried using wp_delete_category() and wp_delete_term() to delete a category, but after the page reloads, the deleted category still shows up in the results of get_categories(). But if I reload the page a second time, the category is gone.

How can I make sure it goes away after the first reload?


I'm grabbing form data with a $_POST and handing to wp_delete_category(). This is all taking place on a plugin settings page. Here's a little snippet of my code. Suggestions welcome.

if ( isset($_POST['deleteSelectedCategories']) ){

    $cats = $_POST['catsToDelete'];
    $catsArray = explode(',', $cats);

    foreach ($catsArray as $catID) {
        // wp_delete_category( $catID );        
        wp_delete_term( $catID, 'category' );
    }
}
Share Improve this question edited Feb 26, 2019 at 15:11 thingEvery asked Feb 26, 2019 at 6:22 thingEverythingEvery 1471 silver badge9 bronze badges 4
  • Do you have any caching plugins active on that site? Does your hosting use any caches? – Krzysiek Dróżdż Commented Feb 26, 2019 at 6:36
  • No caching plugins installed and nothing in the cpanel. Any other changes I make show up right away. Basically, no evidence of caching as far as I can tell. – thingEvery Commented Feb 26, 2019 at 6:59
  • When exactly are you running wp_delete_category()? In what context? – Jacob Peattie Commented Feb 26, 2019 at 10:05
  • @JacobPeattie I'm calling it from a plugin settings page. I've updated my question. – thingEvery Commented Feb 26, 2019 at 15:13
Add a comment  | 

1 Answer 1

Reset to default 0

I've found a workaround by forcing the second refresh, but I'm sure there's probably a better way to do this.

In order to make the process smoother for the user, I'm thinking I'll have to do an AJAX post and then refresh on success to show the updated data.


if ( isset($_POST['deleteSelectedCategories']) ){

    $cats = $_POST['catsToDelete'];
    $catsArray = explode(',', $cats);

    $success = 0;

    foreach ($catsArray as $catID) {
        if ( wp_delete_term( $catID, 'category' ) ) {
            $success++;
        }
    }

    if ($success > 0) {
        echo '[Here I have some HTML to block out the screen 
            and show a message to the user, 
            and then the following meta refresh:] 
            <meta http-equiv="refresh" content="0" />';
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748862331a314340.html

最新回复(0)