Come ottenere l'ID post personalizzato del tipo di post da slug?
3 risposta
- voti
-
- 2015-12-03
Puoi utilizzare
get_page_by_path()
-non lasciartiingannare dalnome,ilterzo argomento èiltipo dipost:if ( $post = get_page_by_path( 'the_slug', OBJECT, 'post_type' ) ) $id = $post->ID; else $id = 0;
You can use
get_page_by_path()
- don't let the name fool you, third argument is the post type:if ( $post = get_page_by_path( 'the_slug', OBJECT, 'post_type' ) ) $id = $post->ID; else $id = 0;
-
- 2015-12-03
Se aspetti unpaio digiornie esegui l'aggiornamento a Wordpress 4.4 ,che sarà rilasciatoil8 dicembre ( AFAIK ),puoi utilizzareilnuovoparametro
post_name__in
inWP_Query
che accetta un array di slugESEMPIO
Se haibisogno dell'oggettopost completo
$args = [ 'post_type' => 'my_custom_post_type', 'posts_per_page' => 1, 'post_name__in' => ['post-slug'] ]; $q = get_posts( $args ); var_dump( $q );
Seti serve solo l'ID
$args = [ 'post_type' => 'my_custom_post_type', 'posts_per_page' => 1, 'post_name__in' => ['post-slug'], 'fields' => 'ids' ]; $q = get_posts( $args ); var_dump( $q );
If you wait a couple of days, and upgrade to Wordpress 4.4 which will be released the 8th of December (AFAIK), you can use the new
post_name__in
parameter inWP_Query
which takes an array of slugsEXAMPLE
If you need the complete post object
$args = [ 'post_type' => 'my_custom_post_type', 'posts_per_page' => 1, 'post_name__in' => ['post-slug'] ]; $q = get_posts( $args ); var_dump( $q );
If you only need the ID
$args = [ 'post_type' => 'my_custom_post_type', 'posts_per_page' => 1, 'post_name__in' => ['post-slug'], 'fields' => 'ids' ]; $q = get_posts( $args ); var_dump( $q );
-
Bello,maprobabilmente unpo 'impegnativoper ottenere unpostper slug?Perché hanno aggiunto quellafunzione !?Nice, but probably a bit intensive to get one post by slug? Why did they add that feature!?
- 0
- 2015-12-03
- TheDeadMedic
-
@TheDeadMedic Mighty è unpo 'intenso sì,non ho davverotestato leprestazioni su questo,solo aggiunto comemezzo di alternativa ;-).Quanto almotivo,davveronon lo so,sembra una di quelle cosebellee inutiliper cui WordPress è cosìfamoso.Anche la convenzione di denominazione,IMHO,è sbagliata,proprio come laproprietà `post_name`e ilparametro` category_name`,che avrebbe dovutoessere `slug`e non`name`@TheDeadMedic Mighty be a bit intensive yes, haven't really tested performance on this, just added as means of an alternative ;-). As to why, I really do not know, looks like one of those nice-to-have-unnecessary things that WordPress is so famous for. The naming convention, IMHO, is also wrong, just like the `post_name` property and `category_name` parameter, which should have been `slug` and not `name`
- 1
- 2015-12-03
- Pieter Goosen
-
Bello :) wowin realtàperme è unbell'aggiornamento :)Nice :) wow actually for me its nice update :)
- 0
- 2015-12-03
- stlawrance
-
@TheDeadMedic Non c'è differenza difuso orariotrailmiometodoe iltuometodo.Ottengo costantementetra 0,002e 0,005 secondipereseguire ciascuna dellenostre query.Inoltre,entrambii metodieseguono solo 1 query.;-)@TheDeadMedic There is no time difference between my method and your method. I constantly get between 0.002 and 0.005 seconds to run each of our queries. Also, both methods run 1 query only. ;-)
- 1
- 2015-12-03
- Pieter Goosen
-
Quando ho suggerito che `get_page_by_path` èpiùperformante,intendevo solo che PHP hameno lavoro dafare (unpo 'di analisi degli argomentie una query SQL) rispetto a` WP_Query` (che coinvolgepiùistanze di oggettie funziona sumoltifiltri/azioni/argomenti).Itempi effettivi raramentetendono aillustrare ameno chenon simuliamomigliaia di operazioni.Ma vale lapena saperlonelmondo reale,allafine nonfa differenza;)When I suggested `get_page_by_path` is more performant I just meant PHP has less work to do (a bit of argument parsing and an SQL query) compared to `WP_Query` (which involves multiple object instantiation and running over many filters/actions/arguments). Actual timings rarely tend to illustrate unless we simulate 1000's of ops. But worth knowing in the real-world it ultimately makes no difference ;)
- 0
- 2015-12-03
- TheDeadMedic
-
@TheDeadMedic * Volevo solo dire che PHP hameno lavoro dafare * - sì,di sicuro,totalmente d'accordo.È semprebello conoscere le alternative (* anche se a voltepotrebbenonessere lamigliore *)e avere una sorta dibenchmark.Godere ;-)@TheDeadMedic *I just meant PHP has less work to do* - yes, for sure, totally agree. It is just always nice to know alternatives (*although it might sometime not be the best*) and having some kind of benchmarks. Enjoy ;-)
- 1
- 2015-12-03
- Pieter Goosen
-
-
`Url_to_postid` è unafunzionemoltoflessibile:puoipassare qualsiasitipo dipermalink/URL completoe ottenere l'IDpostpiù accuratoper quell'URL.Tuttavia,a causa dell'utilizzoflessibile èpiuttosto lento (specialmente conmoltiendpoint/post-type). Sepassi uno slug soloper untipo dipost specifico,èmoltopiù veloce usare `get_page_by_path ()`The `url_to_postid` is a very flexible function: you can pass in any kind of permalink/full URL and get the most accurate post-ID for that URL. However, because of the flexible usage it's quite slow (esp. with lot of endpoints/post-types). If you only pass in a slug for a specific post type, it's a lot faster to use `get_page_by_path()`
- 1
- 2018-03-02
- Philipp
-
Èpossibileprendere l'ID delpost deltipo dipostpersonalizzato solo da slug?
Per quantone sopossiamo ottenere daid usandotitle.Mapuòesserci lo stessotitoloin untipo dipostpersonalizzato,quindipoichégli slug sono unici èpossibile???