// 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();