• Resolved badlydrawnben

    (@badlydrawnben)


    Hi,

    I’m building some functionality that requires the use of counting the user taxonomy terms.

    Am I right in saying that the term count is supposed to update when a user is deleted?
    That doesn’t seem to happen at the moment.

    To recreate:
    Blank installation of WP 5.2.2 and TwentyNinenteen theme.
    If I do a quick an dirty edit of its functions php and add the example ‘Professions’ taxonomy registration code into the ‘init’ hook then it will set it up and I can create two terms ‘Prof A’ and ‘Prof B’
    Create a new user (subscriber) and assign them to Prof A
    Go to Users>Professions and the count for Prof A is 1. – so far so good.
    Delete this new user
    The Count for Prof A still shows as ‘1’.

    Thanks,
    Ben

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter badlydrawnben

    (@badlydrawnben)

    Alternatively, is there some code that I can run manually to ‘recount/refresh’ the user taxonomy count?

    Thanks.

    Plugin Author shawfactor

    (@shawfactor)

    yes this is a problem the issue is that thsi plugin fills in most of the gaps that wordpress has on user taxonomies but not all.

    The saving grace is that the count will fix itself if that term is allocated to another user (as the count does run when users have their terms assigned or removed).

    Anway I will get to adding this feature at some stage but I need to think about how to do it elegantly.

    but in the mean time you could

    $terms = get_terms( ‘your_taxonomy’, array(
    ‘hide_empty’ => false,
    ) );

    foreach ( $terms as $term ) {

    $lh_user_taxonomies_instance->update_count($term->term_id, ‘your_taxonomy’);

    }

    Thread Starter badlydrawnben

    (@badlydrawnben)

    Great thanks – that certainly seems to fix it on the default installation I tried it out on.

    I can add it to an obscure private page and then visit that from the front end when I need to reset to clear out the dummy data I’m testing out the functionality with..

    Thread Starter badlydrawnben

    (@badlydrawnben)

    Sorry, scratch that – I’m having some issues with getting that to work in whichever place I try to hook it into.

    It just returns an error:
    Fatal error: Uncaught Error: Call to a member function update_count() on null

    If I echo $term->$term_id just before that line then I get the list of IDs that I’d expect.

    Thanks,
    Ben

    Plugin Author shawfactor

    (@shawfactor)

    Your problem is probably that you are running this inside a function. If so you need to declare $lh_user_taxonomies_instance as a global before utilising the class object. Thus

    global $lh_user_taxonomies_instance;

    Plugin Author shawfactor

    (@shawfactor)

    @badlydrawnben have you had a chance to look at this?

    Adding thirst line of code at the top of your function should suffice…

    Thread Starter badlydrawnben

    (@badlydrawnben)

    Yes sorry, I didn’t reply back.
    Works great.
    Thank you.

    Plugin Author shawfactor

    (@shawfactor)

    Excellent I’m resolving for good order.

    Pete

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Count not updating when users deleted’ is closed to new replies.