Hi is there anybody who can help me how to find all posts without category and assign the "Uncategorized"? I have more than 7000 posts and 300 of them are without category, how I could assing one?
And another question, how I can assign category to all posts (except bulk edit which doesnt work well with this amount of posts)?
Hi is there anybody who can help me how to find all posts without category and assign the "Uncategorized"? I have more than 7000 posts and 300 of them are without category, how I could assing one?
And another question, how I can assign category to all posts (except bulk edit which doesnt work well with this amount of posts)?
Share Improve this question asked Jan 3, 2017 at 18:26 Karolína VyskočilováKarolína Vyskočilová 4291 gold badge8 silver badges23 bronze badges 4- How do you know that the 300 posts are "without category," and how did they get category-less? Is this what you get from observing the posts on the Front End or in Edit Post, or are you already returning them via function like wp_get_post_categories()? Did you move the posts from a different site or run some kind of operation on them? The answers would be relevant to what kind of fix to advise. – CK MacLeod Commented Jan 3, 2017 at 19:13
- I know how many posts I have and how many are in the category. I have importend them manualy from another database (and some had no category and I forgot to assign). – Karolína Vyskočilová Commented Jan 3, 2017 at 19:19
- Not sure how you're looking at the posts, but, if you use a PHP debugger or plugin like Shortcode Exec PHP, do you see an array with an empty value where a category ID ought to be? Something like Array( [0] => )? – CK MacLeod Commented Jan 5, 2017 at 3:03
- @CKMacLeod yes, empty value – Karolína Vyskočilová Commented Jan 5, 2017 at 8:50
3 Answers
Reset to default 3The current accepted takes posts out of range too. Since it LEFT JOIN on wp_term_relationships without knowing that the first join might be a Category or a Post_tag, it gives false-positive, hence affecting posts that might not be without category.
Inspired from the current accepted answer, change the "37" for your actual wanted category:
INSERT IGNORE INTO wp_term_relationships
(object_id, term_taxonomy_id, term_order)
SELECT p.ID as `object_id`, 37 as `term_taxonomy_id`, 0 as `term_order`
FROM wp_posts p
WHERE p.post_type="post"
AND p.post_status='publish'
AND NOT EXISTS
(
SELECT *
FROM wp_term_relationships rel
JOIN wp_term_taxonomy tax
ON tax.term_taxonomy_id = rel.term_taxonomy_id
AND tax.taxonomy = 'category'
JOIN wp_terms term
ON term.term_id = tax.term_id
WHERE p.ID = rel.object_id
)
You can do it through sql query. Add your category id instead 37.
INSERT IGNORE INTO wp_term_relationships
(object_id, term_taxonomy_id, term_order)
SELECT
wp_posts.ID as object_id,
37 as term_taxonomy_id,
0 as term_order
FROM wp_posts
LEFT JOIN
wp_term_relationships rel
ON wp_posts.ID = rel.object_id
LEFT JOIN
wp_term_taxonomy tax
ON tax.term_taxonomy_id = rel.term_taxonomy_id
AND tax.taxonomy = 'category'
LEFT JOIN
wp_terms term
ON term.term_id = tax.term_id
WHERE wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
AND term.term_id is null
If you don't set any category to your post then it'll automatically be assigned to Uncategorized. You don't need to assign it manually to Uncategorized
And for assigning category to bulk posts you can use wp_set_post_categories()
function. Get all of your posts(which you need to assign category) in an array and run a loop theough the array with
wp_set_post_categories( $post_ID, $post_categories, $append )
passing needed parameter. It will assign the category to your posts.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744769498a4592671.html
评论列表(0条)