Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Add date filter using gravity forms WordPress

  • SOLVED

I'm trying to display posts on a page that are created by Gravity Forms user submission. This part works fine.

What I need to do now is filter those posts to only show during the dates selected. So i have 2 date fields in the Gravity Form, a start date and an end date. What i need to do is have that post show up on the start date and to disappear from the page after the end date. I also need the post to disappear after 14 days max even if the user chose a longer end date.

I <em>think</em> this should be a fairly simple query that says if post is between x date and y date then show post, otherwise don't display.

Any help would be very much appreciated

Answers (4)

2013-03-07

Christianto answers:

Hi Rex,

did you mean by disappear is making post status to draft?
You might need to use [[LINK href="http://codex.wordpress.org/Function_Reference/wp_schedule_event"]]wp cron[[/LINK]] if you need to compare 2 custom field and 14 days limit against current time

please check this code, it will run daily to compare date and update post to draft/publish based on date and 14 days limit.
// create cron that will check for correct published post each day
if (!wp_next_scheduled('g_post_status')) {
wp_schedule_event( time(), 'daily', 'g_post_status' );
}

// uncomment below to clear this cron
// wp_clear_scheduled_hook('g_post_status');

// perform search for post at certain date, and change post to publish for correct time condition
function expire_submitted_posts(){

// custom query argument
$args = array(
'posts_per_page' => -1
);

$meta_start_date_key = 'start_date_key';
$meta_end_date_key = 'end_date_key';
$current_time = time();

$all_posts = get_posts($args);
foreach ($all_posts as $post){

$this_post = array();
$this_post['ID'] = $post->ID;

// get start and end time from custom fields
$start_publish_date = strtotime(get_post_meta($post->ID, $meta_start_date_key, true));
$end_publish_date = strtotime(get_post_meta($post->ID, $meta_end_date_key, true));

// set maximum 14 days from start time
$max_end_time = $start_publish_date + (14 * 24 * 60 * 60);

// we update post to draft if more than 14 days form start or more than end date
if( $current_time > $max_end_time || $current_time > $end_publish_date){
$this_post['post_status'] = 'draft';
wp_update_post( $this_post );
// we publish post if between start and end date
} else if( $start_publish_date < $current_time && $current_time < $end_publish_date){
$this_post['post_status'] = 'publish';
wp_update_post( $this_post );
}
}
}
add_action( 'g_post_status', 'expire_submitted_posts' );


you might need to adjust your post argument (like query only certain category/post type),
$args = array(
'posts_per_page' => -1
);

and changing meta key for start and end date match with gform custom field key
$meta_start_date_key = 'start_date_key';
$meta_end_date_key = 'end_date_key';


let me know the result..
Thanks


Rex Stevens comments:

Thanks, I will be gone on Friday but I will test this on Saturday and let you know. This looks pretty promising though. Thanks!


Rex Stevens comments:

Hmm, this didn't appear to work, all my posts are still there. Maybe i'm doing something wrong? Here is my whole page...


<?php
/**
* Template Name: Free eBook
*
* This template is the default page template. It is used to display content when someone is viewing a
* singular view of a page ('page' post_type) unless another page template overrules this one.
* @link http://codex.wordpress.org/Pages
*
* @package WooFramework
* @subpackage Template
*/

get_header();


$free_ebooks = new WP_Query('category_name=free-ebooks&showposts=-1&order=desc&post_status=publish');
// create cron that will check for correct published post each day

if (!wp_next_scheduled('g_post_status')) {

wp_schedule_event( time(), 'hourly', 'g_post_status' );

}



// uncomment below to clear this cron

// wp_clear_scheduled_hook('g_post_status');



// perform search for post at certain date, and change post to publish for correct time condition

function expire_submitted_posts(){



// custom query argument

$args = array(
'category_name' => 'free-ebooks',
'posts_per_page' => -1
);



$meta_start_date_key = 'download_start_date';

$meta_end_date_key = 'download_end_date';

$current_time = time();



$all_posts = get_posts($args);

foreach ($all_posts as $post){



$this_post = array();

$this_post['ID'] = $post->ID;



// get start and end time from custom fields

$start_publish_date = strtotime(get_post_meta($post->ID, $meta_start_date_key, true));

$end_publish_date = strtotime(get_post_meta($post->ID, $meta_end_date_key, true));



// set maximum 14 days from start time

$max_end_time = $start_publish_date + (14 * 24 * 60 * 60);



// we update post to draft if more than 14 days form start or more than end date

if( $current_time > $max_end_time || $current_time > $end_publish_date){

$this_post['post_status'] = 'draft';

wp_update_post( $this_post );

// we publish post if between start and end date

} else if( $start_publish_date < $current_time && $current_time < $end_publish_date){

$this_post['post_status'] = 'publish';

wp_update_post( $this_post );

}

}

}

add_action( 'g_post_status', 'expire_submitted_posts' );
?>

<!-- #content Starts -->
<?php woo_content_before(); ?>
<div id="content" class="col-full">

<div id="main-sidebar-container">

<!-- #main Starts -->
<?php woo_main_before(); ?>
<div id="main">
<?php
woo_loop_before();

if (have_posts()) { $count = 0;
while (have_posts()) { the_post(); $count++;
woo_get_template_part( 'content', 'page' ); // Get the page content template file, contextually.
}
}

woo_loop_after(); ?>

<!--<h2 class="orange affiliate">Products</h2>-->
<?php
while ($free_ebooks->have_posts()) : $free_ebooks->the_post(); ?>
<div class="affiliate-image"><?php the_post_thumbnail('medium');?></div>
<h3><?php the_title(); ?></h3>
<p class="meta">by <?php echo get_post_meta($post->ID, 'author_name', true); ?></p>
<?php the_content(); ?>
<p><a href="<?php echo get_post_meta($post->ID, 'amazon_url', true); ?>">Get your copy here!</a></p>
<div class="clear">&nbsp;</div>
<?php endwhile;
?>
</div><!-- /#main -->
<?php woo_main_after(); ?>

<?php get_sidebar(); ?>

</div><!-- /#main-sidebar-container -->

<?php get_sidebar( 'alt' ); ?>

</div><!-- /#content -->
<?php woo_content_after(); ?>

<?php get_footer(); ?>

2013-03-05

WisdmLabs answers:

Hi Rex,

Can you please tell me how you display all the posts on page that are created by Gravity Forms user submission? I mean whether you are using a Gravity form AD ON or you have written a code by yourself.
We need to edit that code (required to put date range condition) in query that shows posts. It will be better If you send a link of page(front end) where you Gravity form is displayed. Then I can give code help. Basically, I will require to know in which variables date picker values are saved so that I can use those variables to put condition in query.

If you send login details via PM, it will be most appreciated.


Rex Stevens comments:

so here is my code to query the posts...

new WP_Query('category_name=free-ebooks&showposts=-1&order=desc&post_status=publish');

and here is how i'm displaying it

<?php
while ($free_ebooks->have_posts()) : $free_ebooks->the_post(); ?>
<div class="affiliate-image"><?php the_post_thumbnail('medium');?></div>
<h3><?php the_title(); ?></h3>
<p class="meta">by <?php echo get_post_meta($post->ID, 'author_name', true); ?></p>
<?php the_content(); ?>
<p><a href="<?php echo get_post_meta($post->ID, 'amazon_url', true); ?>">Get your copy here!</a></p>
<div class="clear">&nbsp;</div>
<?php endwhile;



In my form I have the user choose a start date and and end date, so i need the post to only show up between those 2 dates and thats it. Am I making sense? Let me know if I can clarify more

2013-03-05

Arnav Joy answers:

see this article

http://wpsnipp.com/index.php/loop/display-published-posts-between-two-dates/


Rex Stevens comments:

I tried this and it won't work because this handles all posts a date range, I need it to filter based on the individual posts.


Arnav Joy comments:

you can call this in single post as well ,

fetch the two dates and then pass it .

if dates fields are saving in custom field of post then you can use get_post_meta() function to fetch them.

see here for more information

http://codex.wordpress.org/Function_Reference/get_post_meta


Rex Stevens comments:

As i understand that article, this pulls between 2 specific dates, I need this to happen for each post which all have different dates. I need it to query the dates before it pulls the posts. Does this make sense? See my reply to WisdmLabs

2013-03-06

MDan answers:

To add a filter by date, you add either a start_date or end_date parameter--or both--in YYYY-MM-DD format:
E.g. [directory form="14" start_date="2013-03-05 end_date="2013-03-19"]

See this posts:
http://www.gravityhelp.com/documentation/page/Gform_post_data
http://www.gravityhelp.com/documentation/page/Gform_date_max_year


Rex Stevens comments:

I don't think this will work, at least not exactly because the example you show I believe shows the form between a specific date and I just need the posts to show up between certain dates.