Skip Navigation

[Resolved] Custom field date dropdown menu

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 5 replies, has 3 voices.

Last updated by Minesh 11 months, 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2682003
Screenshot 2024-02-06 104359.png

Good morning
I would need to create a dropdown menu to be able to select all the events per day to which I have assigned a custom field with the date.
I was thinking of creating another custom field with the automatic calculation of the shortcode [types field='meeting day' style='text' format='l'][/types] is this correct?
Or is there a better or simpler solution?

#2682047

Nigel
Supporter

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

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

Sorry, could you clarify what you are aiming to do?

You have an event post type, which has a date custom field ("meeting day").

So there are event posts, with custom dates.

You now want to create a "dropdown menu".

This dropdown displays what? Dates? Or Events?

What is the context for the dropdown, i.e. where will it be used? Somewhere in the back end? Somewhere in the front end?

What should happen when somebody uses the dropdown?

#2682051

Hi thanks for your reply.
Yes, we have a post type "events" and a custom field "meeting day" formatted as date and time.
Instead I would like to make a dropdown menu to select only the day starting from that field and therefore it would have Friday, Saturday and Sunday.
The menu would be on an archive page of all events and should allow you to filter and view only the events of the selected day.

#2682186

Minesh
Supporter

Languages: English (English )

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

Do you mean that you created another custom field for example "Day Name" that will hold the day name value (Monday, Tuesday...etc..etc..) based on the event date and then you plan to add this "Day Name" field as custom field filter to your view to filter the results based on the day name. If this is correct - Yes, that should work.

#2682216

Exact.
The problem, however, is that there are 450 events, already loaded, and I would like it to be calculated starting from the date field. It's possible?

#2682231

Minesh
Supporter

Languages: English (English )

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

Here is the shortcode I build that will help you to copy the day name value to your new custom field based on the existing date field.

You can add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
Note: don't forget to activated the code snippet once added:

Important: Uncheck the checkbox "WordPress admin" for the code snippet you add.

 
add_shortcode('add_cfv_for_existing_posts', 'func_add_custom_field_value_for_existing_posts');
function func_add_custom_field_value_for_existing_posts($atts, $content = '') {
  
extract( shortcode_atts( array(
    'post_type' => 'post',
    'from_field_slug' => '',
  	'to_field_slug' => '',
  	'copy' => false,
  	
  	), $atts ) );
  
    $get_posts_args = array(
                  'post_type'  => $post_type,
                  'post_status' => 'publish',
                  'numberposts' => -1);
  
	$found_posts = get_posts( $get_posts_args );
  	
  
  if($copy) {
    
  	foreach($found_posts as $k=>$v):
    	
  		$old_value = get_post_meta($v->ID,'wpcf-'.$from_field_slug,true);
  		update_post_meta($v->ID,'wpcf-'.$to_field_slug,$old_value);
  	 endforeach;
  }else{
    
    foreach($found_posts as $k=>$v):
  		$old_value = get_post_meta($v->ID,'wpcf-'.$from_field_slug,true);
    	$day_name = types_render_field($from_field_slug,array('style'=>'text','format'=>'l','item'=>$v->ID));
    update_post_meta($v->ID,'wpcf-'.$to_field_slug,$day_name);
  	 endforeach;
  }

}

Before running above code snippet I suggest you take full backup of your ***database and website***:

To run above code snippet, add a new page give the title "toolset test page" and save it. Then add the following shortcode to your page:

[add_cfv_for_existing_posts  post_type='agent'  from_field_slug='agent-birth-date'   to_field_slug='day-name']

'
Where:
- Replace post_type value with your original post_type slug
- Replace from_field_slug value with your original existing date field slug
- Replace to_field_slug value with new custom field slug to which you want to copy the day name

Once you add the above shortcode to "toolset test page" update the page and then try to load the "toolset test page" on frontend only one time.

Later, check few posts in backend to know that the "Day Name" field is populated with correct day name based on your existing date field.

#2682260

Perfect, it works!
Thank you so much