$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Custom post type column which compares dates?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Custom post type column which compares dates?

matteradmin7PV0评论

I have a custom post type where the posts have a start date and an end date. These are saved to the database as Unix timestamp/Epoch. I want to create a column for this custom post type called Active where I want to show a green icon when the current date and time is between the start and end date.

How do I pull the metadata and compare them to the current date/time and if true display a green icon, if not a red icon?

I have this function which does almost the same thing but for search results. But I cannot figure out how to use this in a column:

function filter_search_results( $search_query ) {
$time = current_time( 'timestamp' );
    if ( $search_query->is_search ) {
        $search_query->set( 'meta_query', array(
            'relation' => 'AND',
            array(
                'key' => 'visitor-start-date',
                'value' => $time,
                'compare' => '<='
            ),
            array(
                'key' => 'visitor-end-date',
                'value' => $time,
                'compare' => '>='
            ),
        ) );
    }
}
add_action( 'pre_get_posts', 'filter_search_results' );

I have a custom post type where the posts have a start date and an end date. These are saved to the database as Unix timestamp/Epoch. I want to create a column for this custom post type called Active where I want to show a green icon when the current date and time is between the start and end date.

How do I pull the metadata and compare them to the current date/time and if true display a green icon, if not a red icon?

I have this function which does almost the same thing but for search results. But I cannot figure out how to use this in a column:

function filter_search_results( $search_query ) {
$time = current_time( 'timestamp' );
    if ( $search_query->is_search ) {
        $search_query->set( 'meta_query', array(
            'relation' => 'AND',
            array(
                'key' => 'visitor-start-date',
                'value' => $time,
                'compare' => '<='
            ),
            array(
                'key' => 'visitor-end-date',
                'value' => $time,
                'compare' => '>='
            ),
        ) );
    }
}
add_action( 'pre_get_posts', 'filter_search_results' );
Share Improve this question asked Nov 16, 2018 at 9:37 joq3joq3 3813 silver badges21 bronze badges 2
  • A column in the admin? Or on the front end? – Jacob Peattie Commented Nov 16, 2018 at 9:39
  • @JacobPeattie in the admin pages, sorry for not making it clear. – joq3 Commented Nov 16, 2018 at 9:39
Add a comment  | 

1 Answer 1

Reset to default 3

I solved it:

  if ( 'visitor_active' == $column_name ) {
    $start_date = get_post_meta( $post_id, 'visitor-start-date', true );
    $end_date = get_post_meta( $post_id, 'visitor-end-date', true );
    $current_time = current_time( 'timestamp' );
      if ($start_date < $current_time && $end_date > $current_time) {
        echo '<span class="dashicons dashicons-yes" style="color:#75c377;"></span>';
      }
      else {
        echo '<span class="dashicons dashicons-no" style="color:lightgray;"></span>';
      }
  }

Don't know if this is the best way to do it though?

Post a comment

comment list (0)

  1. No comments so far