Skip Navigation

[Resolved] Function to get the term of the current week (CPT)

This support ticket is created 6 years, 11 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.

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+00:00)

This topic contains 8 replies, has 2 voices.

Last updated by romanB-3 6 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#599055

Hello,

I have a CPT "garde" with begining and end dates (custom fields).

I built a funciton that get current time and check if it is in one of those weeks.

Then it gets the term of taxonomy "groupe-de-garde" of the week.

But the function doesn't seem to work, and I'm wondering why ? Would it be an easier way to do this ?

function determiner_semaine_garde($atts) {
	$aujourdhui = current_time( 'timestamp' );

	$query = array(
		'post_type' => 'garde', 
		'posts_per_page' => '-1',
	); 
	$posts = new WP_Query($query);
    
    while($posts->have_posts()) : $posts->the_post();
		$garde_id = $the_post->ID;
		$date_debut = get_post_meta($garde_id, 'wpcf-debut', true);
		$date_fin = get_post_meta($garde_id, 'wpcf-fin', true);

			if (($aujourdhui > $date_debut) && ($aujourdhui < $date_fin)) {
				$groupe_garde = get_the_terms($garde_id, 'groupe-de-garde');
			}
    endwhile;
	
	return $groupe_garde;
}
add_shortcode('quel_groupe_cette_semaine','determiner_semaine_garde');

Thank you very much.

#599173

Nigel
Supporter

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

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

Hi Roman

Where do you intend to use this shortcode?

Without examining it too closely you seem to be getting all of the garde posts and looping through them and testing for the date, and if any of them satisfy the date test, the last one of those that does will return the groupe-de-garde taxonomy terms for that post.

Is that what you intended?

#599181

This is exactly what I intended, yes.
The function is in function.php, and the shortcode is on a page.
Thank you.

#599204

Nigel
Supporter

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

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

Hi Roman

Debugging custom code is outside of our support policy, which is to say, I will look at this, but the queue is very busy today and it will be later when I can come back to it.

In the meantime, I don't see any reason to use the WordPress loop inside your shortcode, and I would switch to using a standard PHP foreach loop to iterate over the posts found by your query.

And then I would add some debugging info at key points in your code to verify that what happens at each step is what is expected.

So add lines like this...

error_log(print_r($posts, true));

...after your query then inspect your debug log to confirm that the expected posts are retrieved etc.

If you do that and there is a particular step that fails which you don't understand, please let me know the details.

#599223

Thank you for your help.

I've tried what you recommend with no result. Here is the code with a standard foreach :

function determiner_semaine_garde($atts) {
	$aujourdhui = current_time( 'timestamp' );

	$args = array(
		'post_type'   => 'garde'
	);
	$posts = get_posts( $args );
	
	foreach( $posts as $post ) {
		$garde_id = $post->ID;
		$date_debut = get_post_meta($garde_id, 'wpcf-debut', true);
		$date_fin = get_post_meta($garde_id, 'wpcf-fin', true);

			if (($aujourdhui > $date_debut) && ($aujourdhui < $date_fin)) {
				$groupe_garde = get_the_terms($garde_id, 'groupe-de-garde');
				error_log(print_r($posts, true));
			}
	}
	
	return $groupe_garde;
}
add_shortcode('quel_groupe_cette_semaine','determiner_semaine_garde');

It still returns 5 results, and I can't understand why : only 1 has the right begin and end dates...

#599831

Nigel
Supporter

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

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

Hi Roman

The error_log you have included above is not very meaningful.

Try something like this:

function determiner_semaine_garde($atts) {
    $aujourdhui = current_time( 'timestamp' );
 
    $args = array(
        'post_type'   => 'garde'
    );
    $posts = get_posts( $args );
     
    foreach( $posts as $post ) {
        $garde_id = $post->ID;
        $date_debut = get_post_meta($garde_id, 'wpcf-debut', true);
        $date_fin = get_post_meta($garde_id, 'wpcf-fin', true);
 
            if (($aujourdhui > $date_debut) && ($aujourdhui < $date_fin)) {
                $groupe_garde = get_the_terms($garde_id, 'groupe-de-garde');

                error_log("the terms of post ID " . $garde_id . " are " . print_r($groupe_garde, true) );
            }
    }
     
    return $groupe_garde;
}
add_shortcode('quel_groupe_cette_semaine','determiner_semaine_garde');

So your code will loop over all "garde" posts.

For each one it will check if the dates match the conditions (starts before today, ends later than today).

If they do it will output the terms of that post to the error log.

The last matching post (by default, the oldest garde post) will return the terms as an array of term objects.

I'm not sure what you are trying to do here as a shortcode should return a string.

#599975

Hello and thank you very much,
In deed with this new error code I get the right post (8642), but the term is "Array" instead of being the term ID or the term name. I tried to implode but it gets me a "impossible to convert to string" fatal error :

[18-Dec-2017 16:47:04 UTC] the terms of post ID 8642 are Array
(
    [0] => WP_Term Object
        (
            [term_id] => 4
            [name] => 2
            [slug] => 2
            [term_group] => 0
            [term_taxonomy_id] => 4
            [taxonomy] => groupe-de-garde
            [description] => 
            [parent] => 0
            [count] => 26
            [filter] => raw
        )

)

What I need to output with the shortcode is the name of the term.

#600200

Nigel
Supporter

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

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

If you want it to return the name of a single term then you should be using wp_get_post_terms.

You need to specify that you want it to return the name field. It will return an array with all of the names of the assigned terms for the specified taxonomy.

If there will only be one term assigned then it is trivial to get that single name to return.

It should be something like this:

function determiner_semaine_garde($atts) {
    $aujourdhui = current_time( 'timestamp' );
  
    $args = array(
        'post_type'   => 'garde'
    );
    $posts = get_posts( $args );
      
    foreach( $posts as $post ) {
        $garde_id = $post->ID;
        $date_debut = get_post_meta($garde_id, 'wpcf-debut', true);
        $date_fin = get_post_meta($garde_id, 'wpcf-fin', true);
  
            if (($aujourdhui > $date_debut) && ($aujourdhui < $date_fin)) {
                $groupe_garde = wp_get_post_terms( $garde_id, 'groupe-de-garde', array("fields" => "names") );
             }
    }
 
    return $groupe_garde[0];
}
add_shortcode('quel_groupe_cette_semaine','determiner_semaine_garde');

There is no special handling for what happens if the posts don't have a term assigned.

#600364

Not quite sure I understood but it works great. I'll go ahead and try to build the complete function on this base.
Thank you very much for your help ; it's been most usefull.