Skip Navigation

[Resolved] Update taxonomy value based on Custom date field.

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by Minesh 5 months ago.

Assisted by: Minesh.

Author
Posts
#2700950

jum

Tell us what you are trying to do?
I have a Taxonomy named Grant status -> open, closed

I have a custom field Final closing date ex value: June 10,2024 .

I want to auto update the taxonomy open or closed based on final closing date.

I referred the save_post documentation and used this code to update the taxonomy based on comparision and made this function as cron event to run daily.

Below is the code i tried but the taxonomy is not updating correctly it showing grant closed for all the posts.

function update_grant_status_auto() {
// Get all posts with the custom field final-closing-date
$args = array(
'post_type' => 'posts', // Replace with your custom post type
'meta_query' => array(
array(
'key' => 'wpcf-final-closing-date',
'compare' => 'EXISTS'
),
),
'posts_per_page' => -1 // Retrieve all posts
);

$query = new WP_Query($args);
// $today = current_time('Y-m-d'); // Get today's date in 'Y-m-d' format

if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$final_closing_date = get_post_meta($post_id, 'wpcf-final-closing-date', true);

if(empty($final_closing_date) || $final_closing_date > time() )
{
wp_set_object_terms($post_id, 'grant-open', 'grant-status-auto');
} else if ($final_closing_date < time()) {
wp_set_object_terms($post_id, 'grant-closed', 'grant-status-auto');
}

}

wp_reset_postdata();
}
}

// Hook the function to a WordPress action, such as 'init' or a scheduled event
add_action('init', 'update_grant_status_auto');

// Optional: Schedule the function to run daily using WP Cron
if (!wp_next_scheduled('update_grant_status_daily_auto')) {
wp_schedule_event(time(), 'daily', 'update_grant_status_daily_auto');
}
add_action('update_grant_status_daily_auto', 'update_grant_status_auto');

Please guide me how to auto update the taxonomy terms based on custom field date value.

#2701018

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As per our support policy, we do not entertain such custom code. If you need further help regarding such custom programming work - you can contact any of our certified partners:
- https://toolset.com/contractors/

However - I've used the get posts instead of WP_Query() function - you can try to use the following function and check if that help you to resolve your issue:

function update_grant_status_auto() {
// Get all posts with the custom field final-closing-date
$args = array(
'post_type' => 'posts', // Replace with your custom post type
'meta_query' => array(
array(
'key' => 'wpcf-final-closing-date',
'compare' => 'EXISTS'
),
),
'posts_per_page' => -1 // Retrieve all posts
);


$found_results = get_posts( $args );

foreach($found_results as $k=>$v):

$final_closing_date = get_post_meta($v->ID, 'wpcf-final-closing-date', true);


if(empty($final_closing_date) || $final_closing_date > time() ){
		wp_set_object_terms($v->ID, 'grant-open', 'grant-status-auto');
} else if ($final_closing_date < time()) {
	wp_set_object_terms($v->ID, 'grant-closed', 'grant-status-auto');
}	

endforeach;
}
#2701835

jum

Hi Minesh,

I have tried the code you provided the grant status taxonomy is updating correctly based on custom field value.

But for testing when i change the value of custom field final closing date and run the code again , the taxonomy is not getting updated it remains the same previous option.

function update_grant_status_auto3() {
// Get all posts with the custom field final-closing-date
$args = array(
'post_type' => 'grant', // Replace with your custom post type
'meta_query' => array(
array(
'key' => 'wpcf-grant-final-closing-date',
'compare' => 'EXISTS'
),
),
'posts_per_page' => -1 // Retrieve all posts
);

$found_results = get_posts( $args );

foreach($found_results as $k=>$v){

$final_closing_date = get_post_meta($v->ID, 'wpcf-grant-final-closing-date', true);

if(empty($final_closing_date) || $final_closing_date > time() ){
wp_set_object_terms($v->ID, 'grant-open', 'grant-status-auto');
} else if ($final_closing_date < time()) {
wp_set_object_terms($v->ID, 'grant-closed', 'grant-status-auto');
}
wp_reset_postdata();
}
}

// Hook the function to a WordPress action, such as 'init' or a scheduled event
add_action('init', 'update_grant_status_auto3');

// Optional: Schedule the function to run daily using WP Cron
if (!wp_next_scheduled('update_grant_status_daily_auto3')) {
wp_schedule_event(time(), 'daily', 'update_grant_status_daily_auto3');
}
add_action('update_grant_status_daily_auto3', 'update_grant_status_auto3');

Please guide me on this issue.

Thanks in advance

#2701839

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - you will have to debug this further as it's beyond the scope of our support policy to offer support for such custom Edits for the CRON related issues.

Maybe you can first check the current code you are using for edit post and try to edit post where you can update the custom field "final closing date" value and check if in both the cases it works and then try to use the code with your CRON.

Or you can get help from any of our certified partners:
- https://toolset.com/contractors/

#2702221

jum

Hi Minesh,

I understand and thanks for your suggestion.

I checked by only running the code you provided(no cron code included). it works only on first time if i run again it not working .

I think the issue might be in taxonomy settings that preventing it from auto updating the terms when it is initially set.

I am not sure about it yet. Do i need to clear the terms checkbox before running the auto update?

Thanks in advance,

Regards
Kaviya

#2702240

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

How are you actually executing the code without CRON?

Can you please share problem URL and steps I will have to follow to execute the code without CRON and send me admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2703531

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I checked the custom code you added but how to run that code snippet?

Do I need to run and add "save_post" action to run that code?

#2704069

jum
run now.png

Hi Minesh,

I use to run the code using run now in the code snippet . it use to rerun the code.

Thats how i will rerun the code for testing, please correct me it if not the case.

Thanks in advance

#2704092

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I see you have almost 25k to 30k grant posts available.

With the function you wrote:

add_shortcode("set_grant_auto_status","update_grant_status_auto_test");
function update_grant_status_auto_test() {
    // Get all posts with the custom field final-closing-date
    $args = array(
        'post_type' => 'grant', // Replace with your custom post type
        'post_status'      => 'publish',
        'meta_query' => array(
            array(
                'key' => 'wpcf-grant-final-closing-date',
                'compare' => 'EXISTS'
            ),
        ),
        'posts_per_page' => -1 // Retrieve all posts
    );

    $found_results = get_posts($args);

    foreach ($found_results as $post) {
        $final_closing_date = get_post_meta($post->ID, 'wpcf-grant-final-closing-date', true);
        
        $final_closing_timestamp = strtotime($final_closing_date);
   
        if (empty($final_closing_date) || $final_closing_timestamp > time()) {
            wp_set_object_terms($post->ID, 'grant-open', 'grant-status-auto', false);
        } else {
            wp_set_object_terms($post->ID, 'grant-closed', 'grant-status-auto', false);
        }
    }
}

I see you are trying to fetch all records and when I checked by adding the debug plugin I found the following fatal error:

PHP Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 20480 bytes) in /home/customer/www/staging47.scientifyresearch.org/public_html/wp-includes/class-wp-term-query.php on line 886

I'm not sure howmany yet but you will have tons of posts where you already setup "grant-final-closing-date" and it should be large number of posts.

I suggest you should try to update the posts from database by firing a query and check if that helps.

In addition to that I found you have the following line of the code added with your code snippet:

$final_closing_timestamp = strtotime($final_closing_date);

Toolset date field stores date field value as Unix Timestamp so I dont think you will have to convert it to time again. Can you disable that above line of code within your code snippet and adjust the code accordingly and check if that help you to resolve your issue.

For example:

add_shortcode("set_grant_auto_status","update_grant_status_auto_test");
function update_grant_status_auto_test() {
    // Get all posts with the custom field final-closing-date
    $args = array(
        'post_type' => 'grant', // Replace with your custom post type
        'post_status'      => 'publish',
        'meta_query' => array(
            array(
                'key' => 'wpcf-grant-final-closing-date',
                'compare' => 'EXISTS'
            ),
        ),
        'posts_per_page' => -1 // Retrieve all posts
    );

    $found_results = get_posts($args);

    foreach ($found_results as $post) {
        $final_closing_date = get_post_meta($post->ID, 'wpcf-grant-final-closing-date', true);
        
        //$final_closing_timestamp = strtotime($final_closing_date);
         
        $final_closing_timestamp = $final_closing_date;

        if (empty($final_closing_date) || $final_closing_timestamp > time()) {
            wp_set_object_terms($post->ID, 'grant-open', 'grant-status-auto', false);
        } else {
            wp_set_object_terms($post->ID, 'grant-closed', 'grant-status-auto', false);
        }
    }
}

#2704297

jum

Hi Minesh,

Thanks for the support.

I will try to update using database query. Is there any document for reference for querying toolset custom field and taxonomy?

Thanks in advance.

#2704298

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

#2705506

jum

Hi Minesh,

Thanks for the support. I will refer those document you shared for reference.