2

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)?

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
    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). 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
    Jan 5, 2017 at 3:03
  • @CKMacLeod yes, empty value Jan 5, 2017 at 8:50

3 Answers 3

2

The 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 
        )
1
  • Great little query that saved me some time, works as intended - thanks a lot.
    – JBoss
    Aug 22, 2021 at 22:12
2

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
2
  • Wow, this is kind of a reply from the future, but chapeau! Really cool and I'm going to save it somewhere for the next time. Apr 4, 2019 at 14:22
  • Super efficient answer! I updated 19.470 posts within 2-3 seconds :) Thanks!!
    – Philipp
    Feb 4, 2020 at 14:34
1

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.

3
  • 2
    Apparently, @kybernaut.cz is dealing with one of those instances where the database, at risk of putting too strong a word on it, has been corrupted during a transfer. That sometimes results in posts with incomplete arguments: Instead of having a previously assigned value - like a category or attachment ID, they end up with an empty value. It sounds like that's what we may be dealing with here: If the post had been normally created, it would get either the default category or Uncategorized (ID = 0 or 1, depending ). Instead, it may have an empty field. See comment above.
    – CK MacLeod
    Jan 5, 2017 at 3:03
  • @CK MacLeond yes, definitely this case but the workaround with wp_set_post_categories() works just fine. Thanks a lot to both. the_dramatist thanks for pointing the fuction out, that is what I was looking for, however - if your db is somehow corrupted you can have empty values (and WP is ok with it), just there is no default way to catch them all Jan 5, 2017 at 8:53
  • I was writing that ` wp_set_post_categories()` will work fine in this case also. So this function will work fine.
    – CodeMascot
    Jan 5, 2017 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.