For all of my CPTs I have set up a checkbox field as below…
• If Post is still relevant, Value should be 0
• If Post has expired, Value should be 1
Each CPT has a different cause for expiry based on its unique data (such as event dates being in the past or product being out of stock), and some CPTs do not expire. I do not want to unpublish posts if expired, I simply want to update the checkbox field value so that it can be used to filter all views across my site whilst mixing different CPTs within the same views.
To do this, I'd like to set a CRON job to loop through the following CPTs and manage this checkbox field based on each CPT and each Posts individual data…
Expiry of these CPTs is optional:
• Posts: Expire IF ( 'Post Expiry Date' ne ' ' AND IS gte 'PAST_DAY()' )
• Charter Packages: Expire IF ( 'Charter Package Available Until' ne ' ' AND IS gte 'PAST_DAY()' ) OR 'Booking Stock Status' eq 'OutOfStock'
Expiry of these CPTs is based on post field values:
• Events: 'Event End Date' gte 'PAST_DAY()'
• Series: 'Series End Date' gte 'PAST_DAY()'
• Opportunities: 'Applications Closing Date' gte 'PAST_DAY()'
• Charter Opportunities: 'Opportunity End Date' gte 'PAST_DAY()' OR 'Booking Stock Status' eq 'OutOfStock'
• Appearances: 'Appearance Expiry Date' gte 'PAST_DAY()'
• Voyages: 'Voyage Embarkation Date' gte 'NOW()' OR 'Booking Stock Status' eq 'OutOfStock'
⁃ Price Options: Expire based on parent Voyage Expiry
Some CPTs (Organisations and Vessels) do not expire so I would likely just set the value in the first CRON job then remove it and have the field value set via CRED from then on.
Are you able to help with setting this up in php?
For reference, I'm running Types 2.2.22, Views 2.5.2 and CRED 1.9.5
I'm unlikely to be able to update easily due to the site already being live.
I do not want to unpublish posts if expired, I simply want to update the checkbox field value so that it can be used to filter all views across my site whilst mixing different CPTs within the same views.
Please read the information here about using "save 0 when unchecked" and searches: https://toolset.com/errata/checkboxes-that-save-0-are-interpreted-as-checked-by-views/
It is best not to save "0" to indicate an unchecked checkbox. Instead, don't save this field key or any value at all. If you want to store two different values to indicate yes/no, consider using a radio button input with values 1 and 2.
Are you able to help with setting this up in php?
I can show you how to get any information you need from Types custom fields and I can give you advice, but the bulk of this code will be your responsibility. Setting up the code to run on a CRON may require assistance from your host.
$query_args = array(
'post_type' => array('post', 'charter-package', 'event'),
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
foreach ( $query->posts as $post ) {
switch( $post->post_type) {
case 'post':
// validate each post here
break;
case 'charter-package':
// validate each charter package post here
break;
case 'event':
// validate each event post here
break;
default:
break;
}
}
}
Set a checkbox field value of "1" on an expired post:
I'm almost there, using the code below, though it only seems to loop though a limited number of posts for each type. In this instance it runs through 2 organisations and around 10 vessels, but I actually have 80+ published vessel posts and around 15 organisation posts.
There could be another plugin or theme file causing interference in the query, or it could be something else going on. Are the missing vessels and organizations private? Are they scheduled to be published in the future? Any other reason they would be inaccessible to a query?
Try creating a custom shortcode that calls the same update_content_relevance function:
Sure, if you want to set up a duplicate site for me that's fine, or you can give me login credentials to the live site and I can create a clone using the Duplicator plugin. I will activate private reply fields here so you can share either credentials here.