type functions from fields custom add_meta_box wordpress group-by custom-post-type custom-fields

functions - wordpress add text field to custom post type



Wordpress wp_query group por campo personalizado (2)

Puedes probar el siguiente código:

<?php function query_group_by_filter($groupby){ global $wpdb; return $wpdb->postmeta . ''.meta_key = "state"''; } ?> <?php add_filter(''posts_groupby'', ''query_group_by_filter''); ?> <?php $states = new WP_Query(array( ''post_type'' => ''observatoire'', ''post_status'' => ''publish'', ''posts_per_page'' => -1, ''meta_key'' => ''state'' )); ?> <?php remove_filter(''posts_groupby'', ''query_group_by_filter''); ?> <select id="" class="" name="siege_pays" > <option value=""></option> <?php while ( $states->have_posts() ) : $states->the_post(); echo "<option value=".get_field_object(''state'').">".get_field_object(''state'')."</option>"; endwhile; ?> </select>

Consulta:

SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = ''observatoire'' AND (wp_posts.post_status = ''publish'') AND (wp_postmeta.meta_key = ''state'' ) GROUP BY wp_postmeta.meta_key = "state" ORDER BY wp_posts.post_date DESC

Tengo que crear un formulario de búsqueda en el que deba completar una selección por estado de los tipos de mensajes personalizados. Algunos de ellos tienen el mismo estado, entonces me gustaría agruparme para tener una lista de estados seleccionada limpia. He hecho esto:

<?php function query_group_by_filter($groupby){ global $wpdb; return $wpdb->postmeta.".meta_key = ''state''"; } ?> <?php add_filter(''posts_groupby'', ''query_group_by_filter''); ?> <?php $states = new WP_Query(array(''post_type'' => ''observatoire'')); ?> <?php remove_filter(''posts_groupby'', ''query_group_by_filter''); ?> <select id="" class="" name="siege_pays" > <option value=""></option> <?php while ( $states->have_posts() ) : $states->the_post(); echo "<option value=".get_field_object(''state'').">".get_field_object(''state'')."</option>"; endwhile; ?> </select>

la consulta de resultado no funciona .. Ayuda por favor :)

''SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = ''observatoire'' AND ((wp_posts.post_status = ''publish'' OR wp_posts.post_status = ''private'') OR wp_posts.post_status = ''private'') GROUP BY wp_postmeta.meta_key = ''siege_pays'' ORDER BY wp_posts.post_date DESC LIMIT 0, 10''


Tuve que hacer algunas mejoras al Código Chirag Patels, para que funcione:

<?php function query_group_by_filter($groupby){ global $wpdb; return $wpdb->postmeta . ''.meta_value ''; } ?> <?php add_filter(''posts_groupby'', ''query_group_by_filter''); ?> <?php $states = new WP_Query(array( ''post_type'' => ''observatoire'', ''post_status'' => ''publish'', ''posts_per_page'' => -1, ''meta_key'' => ''state'' )); ?> <?php remove_filter(''posts_groupby'', ''query_group_by_filter''); ?> <select id="" class="" name="siege_pays" > <option value=""></option> <?php while ( $states->have_posts() ) : $states->the_post(); echo "<option value=".reset(get_post_custom_values(''state'')).">".reset(get_post_custom_values(''state''))."</option>"; endwhile; ?> </select>