assign the specific term from the "specific" taxonomy to all "specific-post-type" posts with the "sepecific-field-option" option selected.

PHOTO EMBED

Fri Apr 12 2024 21:43:56 GMT+0000 (Coordinated Universal Time)

Saved by @Y@sir

// Add this code in your theme's functions.php file or in a custom plugin
2
​
3
function assign_most_popular_term_to_popular_posts() {
4
    // Get all posts of the "ai-tools" post type with "popular-product" selected
5
    $args = array(
6
        'post_type' => 'ai-tools',
7
        'posts_per_page' => -1, // Retrieve all posts
8
        'meta_query' => array(
9
            array(
10
                'key' => 'highlight-product',
11
                'value' => 'popular-product',
12
                'compare' => '=',
13
            ),
14
        ),
15
    );
16
​
17
    $popular_posts = new WP_Query($args);
18
​
19
    if ($popular_posts->have_posts()) {
20
        while ($popular_posts->have_posts()) {
21
            $popular_posts->the_post();
content_copyCOPY

How to use: Place this code in your theme's functions.php file or in a custom plugin. After adding the code, visit your WordPress admin dashboard. You should see a new menu item called "Assign Most Popular Term" in the sidebar. Click on the "Assign Most Popular Term" menu item, and it will display a button. Click the "Assign Term" button to execute the script and assign the "most-popular" term from the "product-types" taxonomy to all "ai-tools" posts with the "popular-product" option selected. This script only assigns the "most-popular" term and doesn't update the "highlight-product" meta field.