0

Is there any way to update the post_content of all posts that contain a specific product_tag and product attribute.

  $my_post = array(
      'post_content'   => 'New text',
  );

// Update the post into the database
  wp_update_post( $my_post );

Update: I've got the query set up to pull the posts I need, now I just need to properly wp_update_post

$args = array (
    'post_type' => 'product',
    'post_status' => 'publish',
    'numberposts' => 10,
    'product_tag' => 'mug',  // CHANGE THE PRODUCT TAG
    'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'pa_chart-type',
                    'field' => 'slug',
                    'terms' => 'nautical'  // CHANGE THE CHART TYPE
                )
            ),

);
$posts = get_posts($args);

print_r($posts);
2
  • Sure. Do a WP_Query to pull those specific posts, then set the content to whatever. (You can append to the existing content, or replace it entirely, whatever you need to do). Possibly related: wordpress.stackexchange.com/questions/22237/…
    – WebElaine
    Feb 20, 2020 at 22:35
  • @WebElaine Thanks! I've updated the post with the query setup how I need, now to combine it with wp_update_post Feb 20, 2020 at 22:49

1 Answer 1

0

You can loop through each of the $posts and update the post_content with update_post_meta.

<?php

foreach( $posts as $p ) {
    update_post_meta( $p->ID, 'post_content', 'YOURCUSTOMEDITS' );
}
?>

edit: if you want to use wp_update_post, it'd look like this:

<?php

foreach( $posts as $p ) {
  $my_post = array(
      'ID'           => $p->ID,
      'post_content' => 'New text',
  );

  // Update the post into the database
  wp_update_post( $my_post );

}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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