Skip Navigation

[Resolved] How can I filter posts by custom field expiration date?

This support ticket is created 7 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by derekB 7 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#443220

Is there any ways to filter out posts based on a custom field I have set on a custom post type?

I created a field called "expiration date" for a deadline on a scholarship.

How can I make it so when the date is expired that the post does not show up within the archive template for the custom post type?

#443224

This is how I ended up making this work but I don't know if this is too hacky, checking each individual post seems a bit overkill in my opinion.

$today = date('F j, Y');
$expiration = types_render_field( 'scholarship-deadline', array('format' => 'F j, Y') );

This is what I have within the loop:

<?php if (strtotime($today) < strtotime($expiration)) { ?>

		<article class="scholarship-listing">
			<div class="container">
				<div class="scholarship-photo"><?php echo $scholarship_photo_link; ?></div>
				<div class="scholarship-meta">
					<?php the_title( '<h3>', '</h3>' ); ?>
					<?php echo $deadline; ?>
					<?php echo $today; ?>
					<?php echo $expiration; ?>
					<?php the_content(); ?>
					<?php echo $scholarship_btn; ?>
				</div>
			</div>
		</article>

		<?php } ?>

Suggestion on how this could be improved would greatly be appreciated.

#443406

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Derek

You can make a custom WordPress Archive (at Toolset > WordPress Archives) for your custom post type, add a Query Filter to test the value of your custom field, then design the output in the Loop Output section.

Read about custom archive pages here: https://toolset.com/documentation/user-guides/normal-vs-archive-views/

Read more about adding Query Filters for custom fields here: https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/ (it describes adding a filter to a view, but it is the same when adding a filter to a custom archive).

I see you have been using Toolset for a long time, so I won't go into any further detail, but if you need help with anything then please ask away...

#443409

Thanks Nigel,

The problem is that I'm not using views on many sites for clients because I've had to go back in and correct issues where the client goes in and toys around with the views unknowingly.

Is it possible to do this query my self through wordpress filters within the arguments i pass to the query loop?

I'm looking into meta_query and comparing meta_value and meta_key but for whatever reason, the date is not working.

<?php
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = '9';

$today = date('F j, Y');
$expiration = types_render_field( 'scholarship-deadline', array('format' => 'F j, Y') );

$scholarship_args = array(
	'post_type' => 'scholarships',
	'posts_per_page' => $per_page,
	'paged' => $current_page,
	'meta_key'     => 'wpcf-scholarship-deadline',
	'meta_value'   => $today,
	'meta_compare' => '<',
);
$scholarships = new WP_Query($scholarship_args);
?>
#443425

for whatever reason, I'm just not seeing any values coming from this custom field.

Array
(
    [post_type] => scholarships
    [posts_per_page] => 9
    [paged] => 1
    [meta_key] => wpcf-scholarship_deadline
    [meta_query] => Array
        (
            [0] => Array
                (
                    [key] => wpcf-scholarship-deadline
                    [value] => October 5, 2016
                    [compare] => >
                )

            [1] => Array
                (
                    [key] => wpcf-scholarship-deadline
                    [value] => October 5, 2016
                    [compare] => <
                )

        )

)

Var Dump

array (size=5)
  'post_type' => string 'scholarships' (length=12)
  'posts_per_page' => string '9' (length=1)
  'paged' => int 1
  'meta_key' => string 'wpcf-scholarship-deadline' (length=25)
  'meta_query' => 
    array (size=2)
      0 => 
        array (size=3)
          'key' => string 'wpcf-scholarship-deadline' (length=25)
          'value' => string 'October 5, 2016' (length=15)
          'compare' => string '>' (length=1)
      1 => 
        array (size=3)
          'key' => string 'wpcf-scholarship-deadline' (length=25)
          'value' => string 'October 5, 2016' (length=15)
          'compare' => string '<' (length=1)
#443426

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Derek

I'll take a look at this after lunch, but it leapt out at me that you are mixing underscores and hyphens in your custom field slug, check the meta_key in your latest post.

#443428

Sorry, I was just testing using underscore before I made the post, I changed it back to just hyphens. That was my mistake.

Oddly enough, if I create my own custom meta box without the use of WP Types where I input the date through a simple text field. This works as expected :/

array (size=5)
  'post_type' => string 'scholarships' (length=12)
  'posts_per_page' => string '9' (length=1)
  'paged' => int 1
  'meta_key' => string 'wpcf-scholarship-deadline' (length=25)
  'meta_query' => 
    array (size=1)
      0 => 
        array (size=3)
          'key' => string 'wpcf-scholarship-deadline' (length=25)
          'value' => string 'October 5, 2016' (length=15)
          'compare' => string '>' (length=1)

Update:

While looking at the database, I see that the date custom field is being stored in unix timestamp. I attempted to use

date('d.m.Y H:i:s');

but that had no difference, still states there are no posts.

#443473

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Derek

I set up a test site and added the code below to my functions.php and it appears to work.

You should double check the slugs I used for the post type and the post meta, and you may want to tweak it to your needs.

/**
 * Add filter to scholarships archive
 */
function filter_scholarships( $query ){
	// only modify the scholarship archive
	if ( is_post_type_archive( 'scholarships' ) ) {

		$now = time();

		$meta_query = array(
			array(
				'key'		=> 'wpcf-scholarship-deadline',
				'value'		=> $now,
				'compare'	=> '>='
			)
		);

		$query->set( 'meta_query', $meta_query );

	}
}
add_filter( 'pre_get_posts', 'filter_scholarships' );

As a footnote, are you familiar with Toolset Embedded? You can create sites with Toolset but ship the site with read-only versions of the Toolset plugins so your clients can't change and break things.

https://toolset.com/documentation/embedded-toolset/

#443484

Thanks Nigel,

I had figured it out once I seen it was stored in unix timestamp value within the database.

I appreciate you taking the time to look into this. Such a pain sometimes and it ends up being an easy solution! DOH!

I didn't know about embedded toolset. I will need to check that out. I'm a lifetime member here and love the tools and support that is provided. You guys do an amazing job.

Again, thanks for the help!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.