Custom post type column which compares dates?












3














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






















  • A column in the admin? Or on the front end?
    – Jacob Peattie
    Nov 16 '18 at 9:39










  • @JacobPeattie in the admin pages, sorry for not making it clear.
    – joq3
    Nov 16 '18 at 9:39
















3














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






















  • A column in the admin? Or on the front end?
    – Jacob Peattie
    Nov 16 '18 at 9:39










  • @JacobPeattie in the admin pages, sorry for not making it clear.
    – joq3
    Nov 16 '18 at 9:39














3












3








3







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













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' );






php customization






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 9:37









joq3

11110




11110












  • A column in the admin? Or on the front end?
    – Jacob Peattie
    Nov 16 '18 at 9:39










  • @JacobPeattie in the admin pages, sorry for not making it clear.
    – joq3
    Nov 16 '18 at 9:39


















  • A column in the admin? Or on the front end?
    – Jacob Peattie
    Nov 16 '18 at 9:39










  • @JacobPeattie in the admin pages, sorry for not making it clear.
    – joq3
    Nov 16 '18 at 9:39
















A column in the admin? Or on the front end?
– Jacob Peattie
Nov 16 '18 at 9:39




A column in the admin? Or on the front end?
– Jacob Peattie
Nov 16 '18 at 9:39












@JacobPeattie in the admin pages, sorry for not making it clear.
– joq3
Nov 16 '18 at 9:39




@JacobPeattie in the admin pages, sorry for not making it clear.
– joq3
Nov 16 '18 at 9:39










1 Answer
1






active

oldest

votes


















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?






share|improve this answer

















  • 1




    Looks fine to me.
    – Jacob Peattie
    Nov 16 '18 at 9:53











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "110"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f319393%2fcustom-post-type-column-which-compares-dates%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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?






share|improve this answer

















  • 1




    Looks fine to me.
    – Jacob Peattie
    Nov 16 '18 at 9:53
















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?






share|improve this answer

















  • 1




    Looks fine to me.
    – Jacob Peattie
    Nov 16 '18 at 9:53














3












3








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?






share|improve this answer












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?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 9:50









joq3

11110




11110








  • 1




    Looks fine to me.
    – Jacob Peattie
    Nov 16 '18 at 9:53














  • 1




    Looks fine to me.
    – Jacob Peattie
    Nov 16 '18 at 9:53








1




1




Looks fine to me.
– Jacob Peattie
Nov 16 '18 at 9:53




Looks fine to me.
– Jacob Peattie
Nov 16 '18 at 9:53


















draft saved

draft discarded




















































Thanks for contributing an answer to WordPress Development Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f319393%2fcustom-post-type-column-which-compares-dates%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?