Query personalizzata con orderby meta_value del campo personalizzato
8 risposta
- voti
-
- 2011-04-24
Puoi definire lameta chiaveperilparametro orderby usandoil vecchiometodo (l'hotestato su 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
Questoproblemain generale viene risoltoin WordPress 4.2 utilizzando query connome.ades.
$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 )));
Perme,volevo ordinarein base a un camponumericoe ho dovuto utilizzare
'type' => 'NUMERIC'
all'interno dellameta query.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.-
Fantastica scopertanelle querynominate!Awesome find on the named queries!
- 1
- 2017-07-30
- Kaji
-
Sì,anche le query connome hanno risposto alla domandaperme.Yep, named queries answered the question for me, too.
- 0
- 2017-07-31
- Dalton Rooney
-
Eccellente.Non sono sicuro del significato di "value"in quella order_clauseperchénon è richiestoin quanto lo ordinerà dal valorepiù alto a quellopiùbasso di "order_in_archive".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
Il codice WP èeffettivamentefonte di confusione quando si affronta questoproblema.
In realtànon ènecessarioilparametrometa_queryper utilizzare orderby,invece utilizzailparametrometa_key,che sebbene da WP Codex sia deprecato è stato stabilito qui: Come usi orderby conmeta_queryin Wordpress 3.1? che orderyby ha ancorabisogno delmeta_key.
così dovrebbeessere
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' ) )
-
Bene,sì,questo èil vecchiometodopereseguire questa querye sembrafunzionareper quella specificata.Ad ognimodo,per querypiù complesse,nonfunzionerà.Ho scoperto che sitratta di unproblemanoto di cui ci si sta occupando,i dettaglipossonoesseretrovatinei tickettrac [# 15031] (http://core.trac.wordpress.org/ticket/15031)e [# 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
Èfacile:
Eccoilmio codice:
query_posts(array( 'post_type' => 'directors', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'director_weight', 'meta_key' => 'director_weight' ) );
Il dettaglioprincipale è:includi
meta_key
,ilmio codicenon è stato ordinato ameno chenon siainclusometa_key
e questo ètutto:Eccoil codice completo di unelenco di
directors
immagini ordinateperdirector_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
Non utilizzare 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' )));
Utilizzai parametri 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
Hotrovato chefunzioni abbastanzabene.
<?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' ) );
-
La domanda riguardailnuovoparametro `meta_query`e ilmodoin cui un` orderby`funziona conesso,e noni parametripiù vecchima ancorafunzionanti `meta_key`/`meta_value`.Inoltre,nonincoraggiamo l'uso di `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
-
Questa risposta contienepiù cattivepratiche: usare -1,passarlo come stringa,usare query_posts.Dovrebbeessere rimosso.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
Avevo una serie di date deglieventipersonalizzate di cui avevobisognoper ordinareper data decrescente.Poiché la data delmioeventopersonalizzatoeramemorizzatanellamiatabella wp_postmetaederain genere diversa dapost_date o dalle datemodificate,questo hafunzionatoperme:
$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);
Puoi quindi scorrere $postin questomodo:
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
Questo lavoroperme,
$args = array( 'post_type' => 'services', 'order' => 'ASC', 'orderby' => 'order_clause', 'meta_query' => array( 'order_clause' => array( 'key' => 'order_in_archive' )));
Necessario soloperfornire la chiaveper 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
Sai,apartire da WP3.0 ci sono opzioniper query avanzatepersonalizzate,il che èfantastico. Apartire da questo,alcuniparametri di query dei campipersonalizzati comemeta_key,meta_value sono stati deprecatiperilnuovoparametrometa_query ( vedi qui )
Cerco di avere una querypiuttosto semplice con lanuova sintassi,interrogarei post di un certopost_type (servizi) che contiene unameta_key specificata (order_in_archive) - questo sta andandobene comeprevisto. Ma - voglio ordinarein base alla querypermeta_valuee senza successo.
Questa è lamia domanda -
Hoprovato orderby anchepermeta_value_numerice meta_value,main ogni casoi risultati vengono ordinatiper data dipubblicazione (comefannoi normalipost). Qualcuno sa comepuòesserefatto?
Grazie