如何找到所有没有分类的帖子,并分配“未分类”?

时间:2017-01-03 作者:Karolína Vyskočilová

嗨,有没有人能帮我找到所有没有分类的帖子并分配“未分类”的帖子?我有7000多个帖子,其中300个是没有类别的,我怎么能找到一个?

还有一个问题,我如何为所有帖子分配类别(批量编辑除外,批量编辑不能很好地处理这么多帖子)?

3 个回复
最合适的回答,由SO网友:MartiNuker 整理而成

当前接受的帖子也超出了范围。由于它在wp\\u term\\u关系上留下了JOIN,而不知道第一个JOIN可能是一个Category或Post\\u标记,因此它给出了误报,从而影响了可能没有Category的帖子。

受当前公认答案的启发,将“37”改为您实际想要的类别:

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 
        )

SO网友:alex

您可以通过sql查询来完成。改为添加类别id 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

SO网友:CodeMascot

如果您没有为您的帖子设置任何类别,那么它将自动分配给Uncategorized. 您不需要手动将其分配给Uncategorized

对于将类别分配给批量帖子,您可以使用wp_set_post_categories() 作用在一个数组中获取所有帖子(需要分配类别),并使用wp_set_post_categories( $post_ID, $post_categories, $append ) 正在传递所需的参数。它将为您的帖子分配类别。