-2

I have a problem! I have my website with wordpress. I only have one category and I want to have 5 categories. The problem is that I have published more than 7000 post. How can I use a word of the content of the post and relationship to one of the new categories.

Example cat (word of the Post_content) ----> cat (new category)

Please, How is the "SQL queries"? Thanks

2 Answers 2

0

I personally find it easier to use built-in WP functions rather than running straight MySQL queries, so here's one option:

<?php
global $wpdb;
// replace 'yoursearchword' with whatever you're looking for.
// the two % symbols are to find the word no matter what comes before or after.
$query = "SELECT * FROM wp_posts WHERE post_content LIKE %yoursearchword%";
$posts = $wpdb->get_results($query);
foreach($posts as $post) {
    // replace with the desired category ID number.
    wp_set_post_categories($post->ID, array(1));
}
?>

For SEO purposes it's best to only assign one category per post, which is what this code achieves. Each time you run it, if your search word is found, all categories will be overwritten by just the one you are assigning. If instead you would like posts to be able to live in multiple categories, add true and the function will append (add additional) categories instead of overwriting.

wp_set_post_categories($post->ID, array(1), true);
5
  • Thanks for the reply. Where add the php code? Thanks
    – Donovan
    Jan 26, 2018 at 17:31
  • It should be noted that array(1) is defining a category ID of 1 - that should be whatever category the user wants to assign. Since it sounds like OP wants to assign any of five potential categories, this may not work. Also @Donovan that code could go in your functions.php in your theme, although once things are updated to a state that you like you should remove it so that it doesn't continue to run on every page load.
    – phatskat
    Jan 26, 2018 at 18:07
  • Hello my code does not work. Please help me make the correct code. If for example: category ID = 9 Category name = test word to search the content = magazine How should it be code? Sorry for the inconvenience, Thanks.
    – Donovan
    Jan 26, 2018 at 19:38
  • To set the category to id 9 whenever you find the word "magazine" in the post content, you would just use the code above - replace %yoursearchword% with %magazine% and replace array(1) with array(9). Easiest way is to place this in a theme file such as single.php then view one of your posts. After it runs, remove the code from your theme file, as the categories are already set and won't need to be changed again.
    – WebElaine
    Jan 29, 2018 at 14:58
  • The code does not work for me. You can do an SQL queries for me. Thanks WebElaine
    – Donovan
    Apr 11, 2018 at 5:33
0

I would not advise you to use a direct SQL approach as you expose yourself to all kinds of data corruption.

Your best bet would be to create a PHP function that would only be run once and do the job. You might run into some timeout issues, but those can be overcome by temporarily setting a really high PHP timeout value.

First of all, you should make sure that the new categories are properly created in WP admin. Then remember their IDs in an array.

To get you started, this is the function that I would use, hooked into WP admin dashboard so it only fires when you go to that page.

function wp1234_attach_posts_to_new_categories() {
    $categories_mapping = array(
        'targetword1' => '101', //this is a category ID
        'targetword2' => '102', //this is another category ID
        'targetword3' => '103', //this is yet another category ID
    );

    // Grab all the posts and cycle through them
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'suppress_filters' => true,
        'ignore_sticky_posts' => true,
        'no_found_rows' => true,
        'cache_results' => false, // there is no need to cache this
    );
    $new_query = new WP_Query();
    foreach ( $new_query->query( $args ) as $post ) {
        if ( ! empty( $post->post_content ) ) {
            foreach ( $categories_mapping as $word => $category_id ) {
                if ( false !== strpos( $post->post_content, $word ) ) {
                    // We have found the word in the content
                    // Attach the post to the category, by appending to existing categories - the last parameter
                    wp_set_post_categories( $post->ID, array( absint( $category_id ) ), true );

                    // We will stop at the first found word
                    break;
                }
            }
        }
    }

    // Leave a sign that we have done something
    print( PHP_EOL . '### ATTACHED POSTS TO CATEGORIES ###' . PHP_EOL );
}
// This action gets run only on the WP admin dashboard page
add_action( 'wp_dashboard_setup', 'wp1234_attach_posts_to_new_categories' );

After you are done, remove the add_action at least so you don't hammer your site.

Let me know if this helps.

4
  • My Friend, Thanks for the reply. Where add the php code? Thanks
    – Donovan
    Jan 26, 2018 at 17:49
  • @Donovan try your theme's functions.php. Also, you could use stripos instead of strpos in case your target word could be in mIxEd CaSe. stripos will match without regard to case.
    – phatskat
    Jan 26, 2018 at 18:10
  • Hello my code does not work. Please help me make the correct code. If for example: category ID = 9 Category name = test word to search the content = magazine How should it be code? Sorry for the inconvenience, Thanks.
    – Donovan
    Jan 26, 2018 at 19:38
  • The code does not work for me. You can do an SQL queries for me. Thanks Vlad Olaru.
    – Donovan
    Apr 11, 2018 at 5:35

Your Answer

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

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