Update all posts by selecting a specific field type.

PHOTO EMBED

Fri Apr 12 2024 19:50:14 GMT+0000 (Coordinated Universal Time)

Saved by @Y@sir #updateall posts #updateall posts by selecting a specific field type

// Add this code in your theme's functions.php file or in a custom plugin
2
​
3
function update_all_ai_tools_posts_highlight_product_meta() {
4
    // Get all posts of the "ai-tools" post type
5
    $args = array(
6
        'post_type' => 'ai-tools',
7
        'posts_per_page' => -1, // Retrieve all posts
8
    );
9
​
10
    $ai_tools_posts = new WP_Query($args);
11
​
12
    if ($ai_tools_posts->have_posts()) {
13
        while ($ai_tools_posts->have_posts()) {
14
            $ai_tools_posts->the_post();
15
​
16
            // Update the "highlight-product" meta field to "none"
17
            update_post_meta(get_the_ID(), 'highlight-product', 'none');
18
        }
19
​
20
        // Reset post data
21
        wp_reset_postdata();
content_copyCOPY

using this script we can Update all posts by selecting a specific field type in any Post Type.