• I have 100+ posts that have the category of Marriage. To all of those posts, I want to add the Category Relationships.

    Is there an efficient way to accomplish this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Yes. You can create a script on you functions.php that will loop through all the posts that has marriage category and add the category relationship.

    Thread Starter Revived

    (@revived)

    LOL, easy as that huh?

    Can someone actually help with a script or plugin? I have no idea how to create such a thing.

    Abdul Samad

    (@abdul-samad-k-t)

    Hi,

    You could use this PHP Script in your functions.php

    add_action('init','add_relationship_category');
    
    // Loop Posts through Marriage category and add Relationship category for each post
    function add_relationship_category () {
    	
    	$args = array (
        'cat' => array(16), // category id for 'Marriage' which you get by hovering over 'Edit' in 'Posts' => 'Categories' page
        'posts_per_page' => -1, 
    );
    
    $cat_posts = new WP_Query($args);
    $post_category = 51; // 'Relationship' category id 
    
    if ($cat_posts->have_posts()) : while ($cat_posts->have_posts()) : $cat_posts->the_post();
    wp_set_post_categories( get_the_ID(), $post_category, 'true' ); 
    endwhile; endif;
    
    }

    And after it works, you can remove the code. ( it only need to work one time )

    • This reply was modified 6 years, 8 months ago by Abdul Samad.
    • This reply was modified 6 years, 8 months ago by Abdul Samad.

    Yeah. Try @abdul-samad-k-t script.

    Just change the WP_query to WP_Query

    Abdul Samad

    (@abdul-samad-k-t)

    @necafasu Thanks! I updated the code.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding a category to all posts that currently have a specific category’ is closed to new replies.