0

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!

8
  • If you really want people to read your code, remove everything redundant like <style> from the code and leave only the essential code. When removed, please, come back.
    – Max Yudin
    Jan 21 at 21:31
  • Read the rules. One question.
    – Max Yudin
    Jan 21 at 21:33
  • This question is 100% correct, will leave as is.
    – DevSem
    Jan 21 at 21:37
  • Three questions in one are out of rules!
    – Max Yudin
    Jan 21 at 21:44
  • I don't know where you're getting three questions from, this is all one question particular to WP_Query just reworded differently. Thanks for downvoting since you don't know how to read.
    – DevSem
    Jan 21 at 21:44

1 Answer 1

0

This has been solved.

add_action('admin_head', 'set_scheduled_today_tag', 99);
function set_scheduled_today_tag() {
    global $post;
    $args = [
        'post_type' => 'wp_events',
        'post_status' => 'any',
        'numberposts' => -1
    ];
    $posts = get_posts($args);
    if ($posts) {
        foreach ($posts as $post) {
            setup_postdata($post);
            $post_date = get_the_date('Y/m/d', get_the_ID());
            date_default_timezone_set('America/Chicago');
            $current_date = date('Y/m/d');
            if ($post_date === $current_date) {
                wp_set_object_terms(get_the_ID(), 'today', 'event_tag', true);
            }
        }
    }
    wp_reset_postdata();
    echo '<style>
            .event_tag-checked {
                background-color: lightblue!important;
            }
            .event_tag-today {
                background-color: #90EE90!important;
            }
          </style>';
}
1
  • Please edit your answer, and add an explanation: why did that solve the problem?
    – fuxia
    Jan 22 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.