Ottieni gli ID dei post da WP_Query?
-
-
Rick,latua domanda è ambigua.Siprega diesseremolto chiari su ciò che si desideraprima diinviare una domanda.Ciò salveràtutti dal rispondere a coseirrilevanti.In realtà haibisogno degli ID delpost restituitiin una stringa,**nonin un array **rick, your question is ambiguous. Please be very clear what you want before posting a question. This will save everyone from answering irrelevant stuff. You actually need the post ID's returned in a string, **not an array**
- 0
- 2014-10-21
- Pieter Goosen
-
Iltuo `wp_reset_postdata` dovrebbeessere dentroe nonfuori dall'istruzioneif,altrimentipotresti reimpostarei dati delpost quandonon sono statimodificatiYour `wp_reset_postdata` should be inside not outside the if statement, otherwise you might reset post data when it hasn't been changed
- 1
- 2014-10-21
- Tom J Nowell
-
Se vuoi sologli ID,dovrestiprendere seriamentein considerazione la risposta di s_ha_dum.Ciò restituiràgli ID senza recuperare anchemolti altri dati dal database chepoibutti via.If you're only wanting the IDs, you should seriously consider s_ha_dum's answer. That will return the IDs without also retrieving lots of other data from the database that you then throw away.
- 1
- 2015-02-27
- Chris Rae
-
4 risposta
- voti
-
- 2014-10-21
-
Questopuòessere utile se haibisogno anche dituttii datiper ognipost,non solo degli ID deipost.Altrimenti,sceglierei la soluzione di @ s-ha-dum.This can be useful if you also need the whole data for each post, not just the posts ids. Otherwise, I'd go with @s-ha-dum's solution.
- 5
- 2017-03-31
- Marian
-
- 2014-10-21
Utilizza l'argomento
fields
nellatua query.campi (stringa) - Quali campi restituire.Tuttii campi vengono restituiti da
predefinito.Ci sono altre due opzioni: - 'ids' - Restituisce un array di IDpost. - 'id=>parent' - Restituisce un array associativo [parent=> ID,…].https://developer.wordpress.org/reference/classes/wp_query/# return-fields-parameter
$latest = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => 3, 'fields' => 'ids' )); var_dump($latest->posts);
Use the
fields
argument in your query.fields (string) - Which fields to return. All fields are returned by
default. There are two other options: - 'ids' - Return an array of post IDs. - 'id=>parent' - Return an associative array [ parent => ID, … ].https://developer.wordpress.org/reference/classes/wp_query/#return-fields-parameter
$latest = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => 3, 'fields' => 'ids' )); var_dump($latest->posts);
-
Questa dovrebbeessere la risposta accettatain quantointerroga solo l'ID rendendolomoltopiù veloce diinterrogaretuttoe di "estrarlo" (loope re-memorizzarlo)in unnuovo array.This should be the accepted answer as it only queries the ID's making it a lot faster than quering everyhing and than 'plucking' (loop and re-storing) it in a new array.
- 16
- 2016-02-05
- Barry Kooij
-
Forse ènecessario otteneregli ID DOPO cheilnormale wp_query ègià statoeseguito,adesempio quando si uniscono due queryin seguitoe sononecessari IDperescluderei risultati dalla queryprecedente.Perhaps you need to get IDs AFTER normal wp_query has already been executed, for example when you merge two queries afterwards and need ID's to exclude results from previous query.
- 0
- 2019-08-05
- trainoasis
-
- 2016-12-28
Usare la soluzione da @ s-ha-dum èeconomico se hai solobisogno di otteneregliide non hai unprecedente set di oggetti query.
Eccoperché:
switch ( $q['fields'] ) { case 'ids': $fields = "$wpdb->posts.ID"; break; case 'id=>parent': $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent"; break; default: $fields = "$wpdb->posts.*";
Perchénel caso specifichi solo
'fields' => 'ids'
non otterrainient'altroin cambio degli ID.Se desideri utilizzare
'fields' => 'id=>parent'
(sembra davvero divertente) otterrai anche l'IDgenitore.Qualsiasi altromodo di utilizzare l'argomento
'fields'
non avrà alcunimpatto apartire da WordPress v4.7.Manel casotu abbia la query comenell'esempio
wp_list_pluck
faràil lavoro.Using the solution from @s-ha-dum is economical if you only need to get the id's, and you don't have previous query object set.
Here is why:
switch ( $q['fields'] ) { case 'ids': $fields = "$wpdb->posts.ID"; break; case 'id=>parent': $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent"; break; default: $fields = "$wpdb->posts.*";
Because in the case you only specify
'fields' => 'ids'
nothing more you will get in return than the ID's.If you would go with
'fields' => 'id=>parent'
(Looks really funny) you will get also the parent ID's.Any other way using
'fields'
argument will not have any impact as of WordPress v4.7.But in case you have the query as in the example
wp_list_pluck
will do the job. -
- 2020-09-03
Suggerisco questa soluzione
get_posts([ 'posts_per_page' => -1, 'post_status' => 'publish', 'post_type' => 'some-custom-post-type', 'fields' => 'ids', ]);
e come ritorno hai un array con ID all'interno;)
array (size=5) 0 => int 81002 1 => int 77885 2 => int 77180 3 => int 74722 4 => int 73312
I suggest this solution
get_posts([ 'posts_per_page' => -1, 'post_status' => 'publish', 'post_type' => 'some-custom-post-type', 'fields' => 'ids', ]);
and as return you have array with ids inside ;)
array (size=5) 0 => int 81002 1 => int 77885 2 => int 77180 3 => int 74722 4 => int 73312
C'è unmodoper recuperare un array di ID dipostinterrogati da quanto segue:
Completamento:
Ho utilizzato
wp_list_pluck
per recuperare un array di IDpost:Quindi ha convertito l'arrayin una stringa utilizzando lafunzioneimplode:
Ci scusiamoper la domanda ambigua.