最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to update post content of all posts with tag?

programmeradmin1浏览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);

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);
Share Improve this question edited Feb 20, 2020 at 22:47 user2059376 asked Feb 20, 2020 at 22:00 user2059376user2059376 1034 bronze badges 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/questions/22237/… – WebElaine Commented 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 – user2059376 Commented Feb 20, 2020 at 22:49
Add a comment  | 

1 Answer 1

Reset to default 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 );

}
?>

发布评论

评论列表(0)

  1. 暂无评论