If a user reaches the ‘Add Listing’ or ‘Add Details’ (/submit_listing/details) page and selects a category, the said category appears to be stored in cache and/or saved as meta irrespective of whether the listing is submitted or not. I assume in an ‘auto-draft’ state.
Hence, each time the page is refreshed or returned to (ie the ‘Add Listing’ button is clicked again), the selected category displays by default. Normally, I would clear values by JS. However, if the selected item is a child or grandchild, the level 0 category items are not included in the HTML and so an error occurs.
Is there a way to clear this pre-selected category completely on all page loads?
(I have noted that disabling HP cache is not enough. I also need to comment out $term_ids = wp_get_post_term( etc. line 82 models/class-post.php. This will clear the selected category and achieve my desired result, but naturally I don’t wish to alter the native plugin file)
WordPress automatically saves auto-drafts in cases like when a user accidentally closes the browser or their device runs out of battery. Uploaded attachments are also stored with the auto-draft. WordPress typically cleans up auto-drafts automatically once a day.
If it’s important to reset the form and there’s no option to let users simply change the category, this is possible but would require custom development. If you’re familiar with coding, here’s a general approach:
You could modify the “Add Listing” button to include a parameter in the URL like ?reset=1, then add a function to the template_redirect hook that checks if the current page is the Add Listing page (you can use hivepress()->router->get_current_route_name() for this). If it is, and if the listing has an “auto-draft” status, you could clear the category using a function like wp_set_post_terms() – Function | Developer.WordPress.org.
Hope this guidance helps.
If you’re not familiar with custom development, I’d recommend reaching out to our Experts for assistance or using our AI assistant to achieve it.
Thanks for the response. Yes, I have experimented with this already. However, in order to use wp_set_post_terms(), I need the ID. I can’t seem to retrieve the post ID using typical functions (eg. get_the_ID() or global variable). How can I source the post ID?
Please try the following snippet. It takes a draft listing and resets its categories. Please note that if it runs on every page refresh, it may prevent users from selecting a category properly:
Thanks for this. It works effectively on page load, but unfortunately the user’s selected categories don’t get saved on submission. I’ve also been experimenting with ‘wp_insert_post’ (hook) and getting a similar result. Can make it work on the render but not on submission.
Please make sure that your code runs only in a single specific case, for example when a special parameter like reset=1 is added to the page URL, as suggested earlier.
Based on the behavior you described, it looks like the code is also running on form submission and therefore resets the categories every time, which causes them to be cleared in 100% of cases.
Thank you. However, had already tried the parameter as formerly instructed. Still doesn’t save the category. My estimation is that the code also runs immediately after selecting the category (when it is saved). Hence, the terms are cleared even though they remain rendered. A similar issue I was having with wp_insert_post using the parameter solution.
Unfortunately, we can’t provide fully custom code tailored to your specific requirements, as it’s outside our support scope. However, if you post the full snippet you’re currently using, we can review it and provide guidance.
Based on what you’re describing, it sounds like there might be an issue with the conditional logic in your code. The snippet shouldn’t be executing when saving a listing unless you’ve modified the original sample code we provided.
On submitting, it appears to run the code before redirecting to the new page. Which means it still complies with the conditionals and clears the categories. I tested it by adding a die() ‘after’ the conditional once the page was loaded. On submitting, the page does in fact die, meaning the code runs again before being directed to the success page.
Yes, that’s what’s happening: the form submission refreshes the same page (which likely has the reset=1 parameter), so it hits the same code again and triggers it repeatedly.
We’d recommend modifying the code to “catch” when the reset=1 parameter is present and then redirect to the listing_submit_details page while removing the reset parameter from the URL.
Essentially, the logic would be: detect that reset=1 is in the URL and the current page is the listing_submit_details page, clear the listing’s categories, then redirect to the listing_submit_details page without the reset=1 parameter in the URL.
If you have a working solution, please feel free to share it with the community! It could be really helpful for others facing similar challenges and would make the forum an even more valuable resource for everyone.
Clear any saved listing category on page load where the ?reset=1 URL parameter exists on the ‘Add Listing’ page; then redirect to the same page minus the parameter.
Create a custom link to the ‘Add Listing’ page which includes the ?reset=1 parameter.
As a note, $listing->set_categories([])->save_categories(); proved to be unreliable. I, therefore, replaced it with wp_set_post_terms() to clear stored meta. No issues since.
Thanks again for your guidance towards the final solution.