简体   繁体   中英

WP: How to flush/reset/commit a wordpress query

I have a really weird problem.

I use wp_insert_category() in wordpress to create a category and retrieve the new id. Then I call another wp_insert_category and pass this id as category_parent parameter to create a child category.

However, doing so works really weird. The category tree is visible in the categories select fields throughout the site, but not in the category list - only the parent cat is visible there. Also it seems that I cannot retrieve the children correctly when using functions like get_posts etc.

I tried creating the category and echo the id and then running the same script again, but creating only the child category with this id hardcoded - IT WORKED!

The only logical explanation to me is that wordpress would cache somehow the queries and would not execute right away the first insert, so that would mess things up when it comes to relations.

Any ideas?

I found a really long discussion here http://core.trac.wordpress.org/ticket/14485

It did not help however. I found a working solution here https://wordpress.stackexchange.com/questions/49634/weird-behaviour-when-adding-terms-term-taxonomies-programmatically

Just call delete_option('taxonomy-slug_children'); between the creation of the parent and the child categories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM