• Resolved kayamurer

    (@kayamurer)


    I want to add a term with an id of 32 of the custom taxonomy hf_cat_ausstellung to posts of a custom post type when a date from acf date picker field has passed.

    I found the following code snippet but can’t get it to work.

    // Expire events
    if ($expireTransient = get_transient($post->ID) === false) {
    	set_transient($post->ID, 'set for 1 minutes', 1 * MINUTE_IN_SECONDS );
    	$today = date('Ymd', current_time('timestamp', 0));
    	$args = array(
    		'post_type' => 'ausstellung',
    		'posts_per_page' => 200,
    		'post_status' => 'publish',
    		'meta_query' => array(
    			array(
    				'key' => 'ende_datum',
    				'value' => $today,
    				'compare' => '<='
    			)
    		)
    	);
    	$posts = get_posts($args);
    	foreach( $posts as $post ) {
    		if(get_field('ende_datum', $post->ID)) {
    			$postdata = array( 'ID' => $post->ID, );
    			$cat_ids = array( 32 );			
    			wp_set_object_terms($postdata, $cat_ids, 'hf_cat_ausstellung');
    		}
    	}
    }
    

    my custom post type is called ausstellung and the custom field has the key ende_datum. I adapted the code from this one here:

    
    // Expire events
    if ($expireTransient = get_transient($post->ID) === false) {
    	set_transient($post->ID, 'set for 1 minutes', 1 * MINUTE_IN_SECONDS );
    	$today = date('Ymd', current_time('timestamp', 0));
    	$args = array(
    		'post_type' => 'ausstellung',
    		'posts_per_page' => 200,
    		'post_status' => 'publish',
    		'meta_query' => array(
    			array(
    				'key' => 'ende_datum',
    				'value' => $today,
    				'compare' => '<='
    			)
    		)
    	);
    	$posts = get_posts($args);
    	foreach( $posts as $post ) {
    		if(get_field('ende_datum', $post->ID)) {
    			$postdata = array(
    				'ID' => $post->ID,
    				'post_status' => 'draft',
    			);
    			wp_update_post($postdata);
    		}
    	}
    }
    

    The above code is working but sets the posts to drafts instead of adding a category.

    I’m a wordpress beginner so maybe anybody has a hint what is wrong with my code.

    • This topic was modified 3 years, 4 months ago by kayamurer.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Try wp_set_object_terms($post->ID, $cat_ids, 'hf_cat_ausstellung');
    Unlike update post, setting object terms only takes the ID itself, not an array.

    I don’t see anything in your code altering the post status. If the post is draft, your query shouldn’t even find the post, hence the term would not even be applied. Since you’re dealing with the current post $post, why are you even querying for posts? Why not apply the term to the current post? Also, what’s causing this code to execute after 1 minute? Is your code in a scheduled event callback?

    Why are you applying a term after 1 minute? Maybe there’s a better approach. I know you said you’re a beginner. Above might sound harsh and critical, that’s not my intent. I want you to think through for yourself what you need to do instead of modifying existing code which may have a different intent. I’m glad you’ve chosen WP and want to be able to customize it further. Don’t be discouraged, it gets easier over time.

    Thread Starter kayamurer

    (@kayamurer)

    Hi bcworkz and thanks for your answer.

    I tried your suggestion but it ins not working either.

    I try to do the following: build a simple event solution with the use of advanced custom fields and custom post types. At the moment i have a custom post type called ausstellungen with acf date picker fields for start and end date of the event. the date is stored in the Ymd format.

    There will be more and more events and things will start to get messy in the admin backend. My idea was to somehow apply a category past-event to the posts automatically when the end-date is over. I will use that category for grouping and filtering in admin columns.

    The above code example is from a post which is about events and marks expired events as drafts, running trough functions.php. I thought i’d be a good starting point as i only know html and css.

    Hi! Thanks for clarifying your use case. This code snippet will schedule an hourly check and apply your category to events that have passed. I’ve thoroughly commented it so you can understand what each part is doing.

    This plugs into the WordPress Cron, which requires a visitor to load a page before it does anything. So if you ever load a page and it looks like past-event hasn’t been applied (and it should have been, more than an hour ago), just refresh the page. Your first page load triggered the task to run. An instant later it should be done.

    // Register a recurring check for past events
    add_action('init', 'wpsf_register_categorize_past_events');
    function wpsf_register_categorize_past_events()
    {
    	// Make sure this event hasn't already been scheduled
    	if (!wp_next_scheduled('wpsf_categorize_past_events')) {
    
    		// Schedule the event
    		wp_schedule_event(time(), 'hourly', 'wpsf_categorize_past_events');
    	}
    }
    
    // Apply past-event category to past events
    add_action('wpsf_categorize_past_events', 'wpsf_categorize_past_events');
    function wpsf_categorize_past_events()
    {
    	// Search for events that aren't yet marked past-event, but they are in the past
    	$ausstellungen = get_posts(array(
    		'post_type' => 'ausstellungen', // only look at events
    		'nopaging' => true, // don't limit the number of results
    		'category__not_in' => array(32), // only bother looking at ones that don't have the past-event category yet
    		'meta_key' => 'ende_datum', // check the custom field
    		'meta_value' => date("Ymd"), // compare with today
    		'meta_compare' => '<', // less than means yesterday and before that (past)
    	));
    
    	// Add the past-event category to each matching event
    	foreach ($ausstellungen as $ausstellung) {
    		wp_set_post_terms($ausstellung->ID, 32, 'hf_cat_ausstellung', true);
    	}
    }
    • This reply was modified 3 years, 4 months ago by rickymccallum87. Reason: Use provided term ID
    • This reply was modified 3 years, 4 months ago by rickymccallum87. Reason: Use provided taxonomy
    Thread Starter kayamurer

    (@kayamurer)

    Hi @rickymccallum87

    I just tested your code and it works exactly as I thought it would.
    Thank you so much for adding the comments also – it is so much more understandable for me what exactly is happening and I can learn and understand how you came to the solution.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add Category After Date Expired’ is closed to new replies.