Consulta personalizada con orderby meta_value de campo personalizado
8 respuestas
- votos
-
- 2011-04-24
Puede definir la clavemetaparaelparámetro orderby usandoelmétodo anterior (probéen WP 3.1.1) ...
query_posts( array( 'post_type' => 'services', 'order' => 'ASC', 'meta_key' => 'some_key', 'orderby' => 'meta_value', //or 'meta_value_num' 'meta_query' => array( array('key' => 'order_in_archive', 'value' => 'some_value' ) ) ) );
You can define the meta key for orderby parameter using the old method (I tested on WP 3.1.1)...
query_posts( array( 'post_type' => 'services', 'order' => 'ASC', 'meta_key' => 'some_key', 'orderby' => 'meta_value', //or 'meta_value_num' 'meta_query' => array( array('key' => 'order_in_archive', 'value' => 'some_value' ) ) ) );
-
- 2016-11-15
Esteproblemaen general se resuelveen WordPress 4.2 medianteel uso de consultas connombre.p.ej.
$args = array( 'post_type' => 'services', 'orderby' => 'order_clause', 'meta_query' => array( 'order_clause' => array( 'key' => 'order_in_archive', 'value' => 'some_value', 'type' => 'NUMERIC' // unless the field is not a number )));
Paramí,quería ordenarpor un camponumérico ytenía que usar
'type' => 'NUMERIC'
dentro de lameta consulta.This issue in general is cleared up in WordPress 4.2 by using named queries. e.g.
$args = array( 'post_type' => 'services', 'orderby' => 'order_clause', 'meta_query' => array( 'order_clause' => array( 'key' => 'order_in_archive', 'value' => 'some_value', 'type' => 'NUMERIC' // unless the field is not a number )));
For me, I wanted to order by a numeric field and I had to use
'type' => 'NUMERIC'
inside the meta query.-
¡Increíble hallazgoen las consultasnombradas!Awesome find on the named queries!
- 1
- 2017-07-30
- Kaji
-
Sí,las consultas connombretambién respondieron lapreguntapormí.Yep, named queries answered the question for me, too.
- 0
- 2017-07-31
- Dalton Rooney
-
Excelente.Noestoy seguro del significado de 'valor'en esa cláusula_ordenporquenoes obligatorio,ya que lo ordenaráporel valormás alto almásbajo de 'orden_en_archivo'.Excellent. I'm not sure the significance of 'value' in that order_clause because it's not required as it will order it by the highest to lowest value of 'order_in_archive'.
- 1
- 2018-12-07
- Andrew Schultz
-
- 2011-04-24
El WP Codexes realmente confuso al abordaresteproblema.
En realidad,nonecesitaelparámetrometa_querypara usarel orderby,sino que usaelparámetrometa_key,que aunque WP Codexestá desaprobado,se haestablecido aquí: ¿Cómo se usa orderby conmeta_queryen Wordpress 3.1? que orderybytodavíanecesitaelmeta_key.
así debería ser
query_posts( array( 'post_type' => 'services', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'order_in_archive' ) )
The WP Codex is actually confusing when addressing this problem.
You do not actually need the meta_query param to use the orderby, instead it uses the meta_key param, which although by WP Codex is deprecated is has been established here: How do you use orderby with meta_query in Wordpress 3.1? that orderyby still needs the meta_key.
so it should be
query_posts( array( 'post_type' => 'services', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'order_in_archive' ) )
-
Bueno,sí,esteeselmétodo antiguopara realizaresta consulta ypareceestarfuncionandoparaestaespecificada.Detodosmodos,para consultasmás complejas,nofuncionará.Encontré queestees unproblema conocido que seestá solucionando,los detalles sepuedenencontraren lostickets detrac [# 15031] (http://core.trac.wordpress.org/ticket/15031) y [# 17065] (http://core.trac.wordpress.org/ticket/17065)Well, yes, this is the old method for doing this query and it appears to be working for this specified one. Anyway, for more complex queries, it won't work. I found this is a known issue that is being taken care of, details can be found in trac tickets [#15031](http://core.trac.wordpress.org/ticket/15031) and [#17065](http://core.trac.wordpress.org/ticket/17065)
- 3
- 2011-04-24
- Maor Barazany
-
- 2017-07-06
Esfácil:
Aquíestámi código:
query_posts(array( 'post_type' => 'directors', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'director_weight', 'meta_key' => 'director_weight' ) );
El detalleprincipales:incluir
meta_key
,mi códigono se ordenó amenos que seincluyemeta_key
,yesoestodo:Aquíestáel código completo de una lista de
directors
imágenes ordenadaspordirector_weight
:<?php query_posts(array( 'post_type' => 'directors', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'director_weight', 'meta_key' => 'director_weight' ) ); while (have_posts()) : the_post(); ?> <li <?php echo get_field('director_weight') ?>> <img src="<?php echo get_field('director_imagen') ?>"> </li> <?php endwhile; wp_reset_query(); ?>
It´s easy:
Here is my code:
query_posts(array( 'post_type' => 'directors', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'director_weight', 'meta_key' => 'director_weight' ) );
The main detail is: include
meta_key
, my code didn´t order unlessmeta_key
is included, and that's all:Here is the full code of a list of
directors
pictures order bydirector_weight
:<?php query_posts(array( 'post_type' => 'directors', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'director_weight', 'meta_key' => 'director_weight' ) ); while (have_posts()) : the_post(); ?> <li <?php echo get_field('director_weight') ?>> <img src="<?php echo get_field('director_imagen') ?>"> </li> <?php endwhile; wp_reset_query(); ?>
-
- 2015-08-07
No use query_posts.
$meta_query = new WP_Meta_Query( $meta_query_args ); $meta_query_args = array( 'post_type' => 'services', 'order' => 'ASC', 'meta_key' => 'your_key', 'orderby' => 'meta_value', //or 'meta_value_num' 'meta_query' => array( array('key' => 'order_in_archive', 'value' => 'some_value' )));
Utilice los parámetros de WP_Meta_Query
Don't use query_posts.
$meta_query = new WP_Meta_Query( $meta_query_args ); $meta_query_args = array( 'post_type' => 'services', 'order' => 'ASC', 'meta_key' => 'your_key', 'orderby' => 'meta_value', //or 'meta_value_num' 'meta_query' => array( array('key' => 'order_in_archive', 'value' => 'some_value' )));
Use the WP_Meta_Query parameters
-
- 2012-12-28
Encontré queestofuncionabastantebien.
<?php query_posts( array( 'posts_per_page' => '-1', 'post_type' => 'services', 'order' => 'DESC', 'meta_key' => '_order', 'orderby' => 'meta_value_num', //or 'meta_value_num' ) );
I found this to work quite well.
<?php query_posts( array( 'posts_per_page' => '-1', 'post_type' => 'services', 'order' => 'DESC', 'meta_key' => '_order', 'orderby' => 'meta_value_num', //or 'meta_value_num' ) );
-
Lapreguntaes sobreelnuevoparámetro `meta_query` y cómo obtener un` orderby`paratrabajar con él,yno sobre losparámetros `meta_key`/`meta_value`más antiguospero aúnfuncionales.Además,nofomentemosel uso de `query_posts`.The question is about the new-ish `meta_query` parameter and getting an `orderby` to work with it, and not about the older but still functional `meta_key`/`meta_value` parameters. Also, let's not encourage the use of `query_posts`.
- 3
- 2012-12-28
- s_ha_dum
-
Esta respuesta contienemúltiplesmalasprácticas: usar -1,pasarlo como una cadena,usar query_posts.Deberíaeliminarse.This answer contains multiple bad practices: using -1, passing it as a string, using query_posts. It should be removed.
- 0
- 2019-03-01
- Ihor Vorotnov
-
- 2017-01-19
Tenía un conjunto defechas deeventospersonalizados quenecesitaba ordenarporfecha descendente.Dado que lafecha demi eventopersonalizado se almacenóen mitabla wp_postmeta y,por logeneral,era diferente a lafecha depublicación o lasfechasmodificadas,estofuncionóparamí:
$args = array( 'post_type' => 'events', // my post type - yours can be 'posts' 'post_status' => 'publish', // only get posts with this status 'orderby' => 'meta_value', // orderby the meta_value of the following meta_key 'meta_key' => 'my_custom_eventdate', // the custom meta_key name 'order'=> 'DESC' // sort descending ); $posts = new WP_Query($args);
Luego,puedes recorrer $publicaciones así:
foreach($posts->posts as $p){ $post_id = $p->ID; // and so on ... // # example of how I retrieve my event date $event = get_post_meta($post_id, 'my_custom_eventdate', true); }
I had a set of custom event dates I needed to sort by date descending. Since my custom event date was stored in my wp_postmeta table, and was typically different to the post_date or modified dates, this worked for me:
$args = array( 'post_type' => 'events', // my post type - yours can be 'posts' 'post_status' => 'publish', // only get posts with this status 'orderby' => 'meta_value', // orderby the meta_value of the following meta_key 'meta_key' => 'my_custom_eventdate', // the custom meta_key name 'order'=> 'DESC' // sort descending ); $posts = new WP_Query($args);
You can then loop through $posts like so:
foreach($posts->posts as $p){ $post_id = $p->ID; // and so on ... // # example of how I retrieve my event date $event = get_post_meta($post_id, 'my_custom_eventdate', true); }
-
- 2019-03-17
Estofuncionaparamí,
$args = array( 'post_type' => 'services', 'order' => 'ASC', 'orderby' => 'order_clause', 'meta_query' => array( 'order_clause' => array( 'key' => 'order_in_archive' )));
Soloesnecesarioproporcionar la clavepara order_clause
This work for me,
$args = array( 'post_type' => 'services', 'order' => 'ASC', 'orderby' => 'order_clause', 'meta_query' => array( 'order_clause' => array( 'key' => 'order_in_archive' )));
Only needed to provide the key for the order_clause
Ya sabes,apartir de WP3.0 hay opcionespara consultas avanzadaspersonalizadas,lo cualesgenial. Apartir deestemomento,algunosparámetros de consulta de campospersonalizados comometa_key,meta_value quedaron obsoletosparaelnuevoparámetrometa_query ( ver aquí )
Intentotener una consultabastante simple con lanueva sintaxis,consultarpublicacionespor un determinadotipo depublicación (servicios) que contiene unameta_claveespecífica (orden_en_archivo);esto vabien como seesperaba. Pero,quiero ordenarpor la consultaporelmeta_valor,y sin éxito.
Estaesmi consulta -
Probé orderbytambiénpormeta_value_numeric ymeta_value,peroen cualquier caso,los resultados se ordenanpor lafecha depublicación (como lo hacen laspublicaciones regulares). ¿Alguien sabe cómo sepuede haceresto?
Gracias