wp query – WP_Query outputs wrong post in custom post type

Question

So I’m working directly in the Admin Dashboard of my website, below I will explain what I’m attempting to achieve and what issues I’m getting.

Here is the code:

add_action('admin_head', 'set_scheduled_today_tag', 99);
function set_scheduled_today_tag() {
    $query = new WP_Query([
        'post_type' => 'wp_events',
        'post_status' => ['schedule'],
        'posts_per_page' => -1
    ]);
    if ($query->have_posts()):
        while ($query->have_posts()):
            $query->the_post();
            $post_date = get_the_date('Y/m/d', get_the_ID());
            $current_date = date('Y/m/d');
            if ($post_date === $current_date) {
                wp_set_post_terms(get_the_ID(), 'Today', 'event_tag', true);
            }
        endwhile;
    endif;
    wp_reset_postdata();

    echo '<style>
            .event_tag-checked {
                background-color: lightblue!important;
            }
            .event_tag-today {
                background-color: #90EE90!important;
            }
          </style>';
}

Here is what I’ve completed:

  • When the post “Date” matches the current date, append a “Today” tag to the post tag and change the color.

enter image description here

Here is the issue:

Before this implementation, I had no problems, but now when I click into a specific post, I get a different post that displays.

  • If I hover over the Koe Wetzel post, it comes back with Post ID: 6608 – This post doesn’t even exist in the DB.
  • When I open the post, it leads me to a random event under my wp_events post_type such as Weird Al Yankovic: Unfortunate Return of the Ill-Advised Vanity Tour.

Questions:

  • Does anyone know what I might be doing wrong with my query?
  • Am I properly querying posts on the admin post_type side of things?
  • Can I query the posts differently?

All help will be appreciated!

0
DevSem 7 months 2022-01-21T15:49:16-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse