Saltar navegación

[Resuelto] How to sort in view birthday data by month?

This support ticket is created hace 1 mes, 2 semanas. 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
- 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)

Este tema contiene 6 respuestas, tiene 1 mensaje.

Última actualización por Rostislav Avtodiychuk hace 1 mes, 2 semanas.

Asistido por: Minesh.

Autor
Mensajes
#2804399

Tell us what you are trying to do? I have custom post type with custom field - happy birthday.
I need sort them by month, for example by "April" and it show me all people who have birthday in april in ascending order by day
Is there a similar example that we can see?
1 april 1973
3 april 1985
15 april 1965
17 april 1978
25 april 2001
29 april 1999
30 april 1996
What is the link to your site? enlace oculto

#2804443

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you want to list post whose birthday falls in current month or you want to use view's shortcode attribute so that you can pass the birthday month?

For example:

[wpv-view name="my-view-showbirthdays" month="April"]

[wpv-view name="my-view-showbirthdays" month="May"]
#2804469

Ideally I need a list of people with their birthdays for a specified month, for example April or June. If it was possible to select a month and filter all people, that would be great.
Sorted by: April
1 april 1973 King Martin
3 april 1985 Coleman Rhys
15 april 1965 King Vernon
17 april 1978 Wilson Mario
25 april 2001 Garcia Anderson
29 april 1999 Gonzalez Brentley
30 april 1996 Foster Amir

#2804477

enlace oculto i made view but it sorted by year

#2804484

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

There is no native way to do it but I've created the following view in legacy mode:
=> enlace oculto

And added the following code to "Custom Code" section offered by Toolset with the code snippet namely "filter-for-birthday-in-month":
=> enlace oculto

add_filter('wpv_filter_query','func_remove_birthday_args', 10, 3);
function func_remove_birthday_args($query_args, $view_settings,$view_id) {
  
  	 if ($view_id == 2220) {
       
       $query_args['meta_query'] = array();
       
       
     }
  	  return $query_args;
}

add_filter( 'posts_where' , 'func_filter_birthday_in_month',99,1);
function func_filter_birthday_in_month( $where ) {
    global $WP_Views;
    global $wpdb;
  
    if($WP_Views->current_view == 2220 and isset($_GET['wpv-birth-month']) and is_numeric($_GET['wpv-birth-month']) ){
          
      		$target_field = 'wpcf-happy-birthday';
            $selected_month = $_GET['wpv-birth-month'];
      
      
           $where .= " And ".$wpdb->prefix."posts.ID IN  (SELECT post_id FROM ".$wpdb->prefix."postmeta
                  WHERE (".$wpdb->prefix."postmeta.post_id = ".$wpdb->prefix."posts.ID)
                  AND ".$wpdb->prefix."postmeta.meta_key='".$target_field."'
                  AND month(FROM_UNIXTIME(".$wpdb->prefix."postmeta.meta_value)) =$selected_month)";
 
           
   
    }
    return $where;
}

You can see the result on the frontend:
- enlace oculto

#2804488

Thank you fro help. Can the birthday day go to data order?
Green Xuan == 02.04.1987
Foster Amir == 03.04.1986
Garcia Anderson == 05.04.1978
King Vernon == 06.04.1994
Gonzalez Ishaan == 11.04.1975
Gonzalez Brentley == 13.04.1981
King Martin == 17.04.1987
Diaz Case == 18.04.1974
Wilson Mario == 20.04.1988
Nelson Woodrow == 28.04.2001
Edwards Nathaniel == 30.04.2001

#2804553

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Can you please check now: enlace oculto

I've added the following filter hook to "Custom Code" section offered by Toolset with the code snippet namely "filter-for-birthday-in-month":
=> enlace oculto

add_filter( 'wpv_filter_query_post_process', 'func_filter_parent_none_post', 10, 3 );
function func_filter_parent_none_post( $query, $view_settings, $view_id ) {
    if ( $view_id ==2220 and !empty( $query->posts ) ) {
      
          
      $all_posts = $query->posts;
      $target_field = 'wpcf-happy-birthday';
      
       $result = array();
       
     foreach($all_posts as $k=>$v):
        $this_date = get_post_meta($v->ID,$target_field,true);
        $this_day = date("d",$this_date);
         
        $result[$v->ID] = $this_day;
        $posts_array[$v->ID] = $v;
      endforeach;
       
      asort($result);
      
      $all_posts = array_keys($result);
     
      	$final_result = array();
     	foreach($all_posts as $k=>$v):
      		if(array_key_exists($v,$posts_array)){
              	$final_result[] = $posts_array[$v];
			}
      	endforeach;
      
        $query->posts = $final_result; 
        $query->found_posts = count($final_result); // modify the count of found posts
        $query->post_count = count($final_result); // modify the count of displayed posts
    }
    return $query;
}

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#benefits-of-adding-custom-code-using-toolset
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

#2804635

Thank you very much for your help, really save me a lot of time to sort people. I will read such documentation and advance my level of knowledge toolset