Post-IDs von WP_Query erhalten?
-
-
Rick,deine Frageistnichteindeutig.Bittemachen Sie sichganz klar,was Sie wollen,bevor Sieeine Frage stellen.Dieserspartjedem die Beantwortungirrelevanter Fragen.Siebenötigen die Post-IDstatsächlichin einer Zeichenfolge,**nichtin einem 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
-
Ihre `wp_reset_postdata` sollte sichinnerhalb undnicht außerhalb derif-Anweisungbefinden,andernfalls können Sie Post-Daten zurücksetzen,wenn sienichtgeändert wurdenYour `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
-
Wenn Sienur die IDsmöchten,sollten Sie die Antwort von s_ha_dumernsthaftin Betracht ziehen.Dadurch werden die IDs zurückgegeben,ohne dass auch viele andere Daten aus der Datenbank abgerufen werden,die Sie dann wegwerfen.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 Antworten
- Stimmen
-
- 2014-10-21
-
Dies kannnützlich sein,wenn Sie auch diegesamten Datenfürjeden Beitragbenötigen,nichtnur die Beitrags-IDs.Ansonsten würdeichmichfür die Lösung von @ s-ha-dumentscheiden.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
Verwenden Siein Ihrer Abfrage das Argument
fields
.Felder (Zeichenfolge) - Welche Felder zurückgegeben werden sollen.Alle Felder werden von
zurückgegeben Standard.Esgibt zwei weitere Optionen: - 'ids' - Gibtein Array von Post-IDs zurück. - 'id=>parent' - Gibtein assoziatives Array [parent=> ID,…] zurück.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);
-
Dies sollte die akzeptierte Antwort sein,danur die IDs abgefragt werden,was sie viel schnellermacht als das Abfragen von allem und als das "Zupfen" (Schleifen underneutes Speichern)in einemneuen 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
-
Möglicherweisemüssen Sie IDs abrufen,nachdem dienormale wp_querybereits ausgeführt wurde,z. B. wenn Sie anschließend zwei Abfragen zusammenführen und IDsbenötigen,um Ergebnisse von der vorherigen Abfrage auszuschließen.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
Die Verwendung der Lösung von @ s-ha-dumist wirtschaftlich,wenn Sienur die IDs abrufenmüssen und kein vorheriges Abfrageobjektfestgelegt wurde.
Hierist der Grund:
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.*";
Weil Sienur
'fields' => 'ids'
Sieerhaltennichtsmehr als die IDs.Wenn Siemit
'fields' => 'id=>parent'
(sieht wirklich lustig aus) Sieerhalten auch die übergeordneten IDs.Eine andere Verwendung des Arguments
'fields'
hat ab WordPress v4.7 keine Auswirkungen.Falls Siejedoch die Abfrage wieim Beispiel haben,erledigt
wp_list_pluck
die Aufgabe.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
Ich schlage diese Lösung vor
get_posts([ 'posts_per_page' => -1, 'post_status' => 'publish', 'post_type' => 'some-custom-post-type', 'fields' => 'ids', ]);
und als Rückgabe haben Sieein Arraymit IDs;)
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
Gibteseine Möglichkeit,ein Array von Post-IDs abzurufen,die von denfolgenden abgefragt wurden:
Follow-up:
Ich habe
wp_list_pluck
verwendet,umein Array von Post-IDs abzurufen:Konvertieren Sie dann das Arraymit derimplodierenden Funktionin einen String:
Entschuldigen Sie diemehrdeutige Frage.