0

Is there a way to bulk tag all posts which have for instance "bottle" in their titles? Thanks!

1 Answer 1

1

Did a little bit of searching around this site and found some links that should help you achieve your goal

1) Search all posts that match a specific title - Found this nifty code on another thread here - A use case example of the above function here

2) Updating the "tags" for a specific post is fairly simple with this function:

  wp_set_object_terms($post_id, 'mytagname', 'post_tag', true);

3) When you put everything together, it should look something like this (this code is untested)

    add_filter( 'posts_search', '__search_by_title_only', 500, 2 );

$query = new WP_Query(
    array(
        's'         => 'title_to_search_for'
    )
);

remove_filter( 'posts_search', '__search_by_title_only', 500 );

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) :
     $post_id = get_the_id();
        wp_set_object_terms($post_id, 'tag_to_add', 'post_tag', true);
    endwhile;

    wp_reset_postdata();
endif;

Good luck!

2
  • Hi. Thanks for your detailed reply! The problem is that I have thousand and thousand articles so the website gave me a "504 Gateway Time-out". Is there a way to achieve my goal via SQL command in phpmyadmin ?
    – job78
    Jul 16, 2014 at 13:53
  • '504 Gateway Time-Out' means the php script ran longer than the allotted execution time. use 'set_time_limit()' to increase the execution time, and the script will run fine. you should avoid direct query calls to wp db unless you really know what you're doing.
    – stoopkid1
    Jul 17, 2014 at 0:30

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.