Wie erhalte ich eine Post-ID (Seite oder CPT) aus einem Titel oder einer Schnecke?
-
-
Wie haben Sie "get_page_by_title" verwendet undist derbenutzerdefinierte Beitragstyp hierarchisch? Ichglaubenicht,dass "get_page" (aufgerufen von "get_page_by_title")fürnicht hierarchische Typenfunktioniert.How were you using `get_page_by_title` and is the custom post type hierarchal, i don't think `get_page`(called by `get_page_by_title`) will work for non-hierarchal types.
- 0
- 2011-03-06
- t31os
-
Ja,derbetreffendebenutzerdefinierte Beitragstypistnicht hierarchisch.Yes, the custom post type in question is non-hierachal.
- 0
- 2011-03-06
- jnthnclrk
-
Ignoriere vorherigen Kommentarbezüglich der Hierarchie,das sollte keine Rolle spielen. Wie hast du `get_page_by_title` aufgerufen?Ignore previous comment regarding being hierarchal, that shouldn't matter, how were you calling `get_page_by_title`?
- 0
- 2011-03-06
- t31os
-
5 Antworten
- Stimmen
-
- 2011-03-06
Sie können diese Funktion verwenden,die von Google "Beitragnach Titel abrufen" springt
/** * Retrieve a post given its title. * * @uses $wpdb * * @param string $post_title Page title * @param string $post_type post type ('post','page','any custom type') * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. * @return mixed */ function get_post_by_title($page_title, $post_type ='post' , $output = OBJECT) { global $wpdb; $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type)); if ( $post ) return get_post($post, $output); return null; }
you can use this function that jumps by google "get post by title"
/** * Retrieve a post given its title. * * @uses $wpdb * * @param string $post_title Page title * @param string $post_type post type ('post','page','any custom type') * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. * @return mixed */ function get_post_by_title($page_title, $post_type ='post' , $output = OBJECT) { global $wpdb; $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type)); if ( $post ) return get_post($post, $output); return null; }
-
Bisher habeichesgeschafft,die Verwendung von DB-Abfragen zu vermeiden,daich lieber Standard-WP-Funktionen verwende.Ist dies wirklich dereinzige Weg,um das zuerreichen,wasichbrauche?So far I've managed to avoid using DB queries, as I prefer to use standard WP functions. Is this really the only way to achieve what I need?
- 1
- 2011-03-06
- jnthnclrk
-
Der veröffentlichte Codeist so ziemlicheine direkte Kopie der Funktion "get_page_by_title" (http://core.trac.wordpress.org/browser/tags/3.1/wp-includes/post.php#L3162),dem HauptunterschiedHierist,dassesfürnicht hierarchische Typenfunktioniert,also lohntes sich,esin Ihrer Funktionssammlung zu haben.The code posted is pretty much a direct copy of the `get_page_by_title` [function](http://core.trac.wordpress.org/browser/tags/3.1/wp-includes/post.php#L3162), the major difference here is that it'll work for non-hierarchal types, so it's one worth having in your function collection.
- 0
- 2011-03-06
- t31os
-
@t31os Ich habenie gesagt,dassesmeinsist. Ich habenur das $post_typepaean hinzugefügt und yepesgehtin meine Sammlung.@t31os I never said it was mine i only added the $post_type paean to it and yep its going to my collection.
- 0
- 2011-03-06
- Bainternet
-
Ich habe dirnichts vorgeworfen,Kumpel,wenn überhaupt,unterstützeich deine Antwort ..;) Du hastbereitsmeine +1 .. :)I wasn't accusing you of anything mate, if anything i'm supporting your answer.. ;) You already got my +1.. :)
- 0
- 2011-03-06
- t31os
-
@t31os Ich weiß,esistnurein schlechter Witz :)@t31os I know ,its just a bad joke :)
- 0
- 2011-03-06
- Bainternet
-
Wie unterscheidet sich das vonnativemget_page_by_title ()? ..get_page ()ist so ziemlich der Alias vonget_post (). Ich sehenicht,wie sich Änderungen aufirgendetwas auswirken.How is this different from native `get_page_by_title()`?.. `get_page()` is pretty much alias of `get_post()` I don't see how change impact anything.
- 0
- 2011-03-06
- Rarst
-
@Rarst Nun,da Sie darauf hingewiesen haben,dasses dasgleicheist,kannich den Unterschiednicht sehen.@Rarst Now that you pointed that out it is the same, i can't see the difference.
- 0
- 2011-03-06
- Bainternet
-
@t31os also,wo kommt dasnicht-hierarchische,was duerwähnt hastins Spiel,wenn die Funktionim Wesentlichengleichist?@t31os so where does _non-hierarchical_ thing you mentioned comes into play if function is essentially same?..
- 0
- 2011-03-06
- Rarst
-
Ich weißnicht,wasich heute Morgengedacht habe,get_page ruft sowiesoget_post auf,undich habe keine Ahnung,warumich dachte,get_pagefunktioniertnurfür hierarchische Typen. Ichglaubenicht,dass das soist,wieespassiert.Don't know what i was thinking this morning, get_page calls get_post anyway, and i have no clue why i thought get_page only works for hierarchal types, i don't think that's true as it happens.
- 0
- 2011-03-06
- t31os
-
Dies kanneinfachmit dem Seitenobjekt durchgeführt werden.Siehemeine Antwort unten.This can be done easily with the page object. See my answer below.
- 0
- 2017-07-29
- Phill Healey
-
- 2011-06-17
Ihnenfehltein Funktionsparameter.Wenn Siefür die zweite optionale Ausgabe des Parameters $eine Nulleingeben,sollte diese Funktionfür Siefunktionieren.
get_page_by_title('My post title', null, 'customposttype');
Ichbin gerade auf dasselbe Problemgestoßen und habe durch Hinzufügen der Null das Problemfürmichbehoben.
You are missing a function parameter. Throw a null in for the second optional paramter $output and this function should work for you.
get_page_by_title('My post title', null, 'customposttype');
I just ran into the same issue and adding the null fixed it for me.
-
Diese "Null" sollte aus Gründen der Klarheit wirklich "OBJEKT" sein.Aberja,er warfim Wesentlichen den Beitragstyp als Ausgabetypein,indemer den Ausgabetyp wegließ.That `null` should really be `OBJECT` for clarity. But, yes he was essentially throwing the post type in as the output type by leaving out the output type.
- 0
- 2017-07-29
- Phill Healey
-
- 2011-06-17
Ichneige dazu,direkte DB-Abfragen zu scheuen.Stattdessen verwendeich das Objekt
WP_Query
,um Dingefürmich zu analysieren.Diesistim Grundeeine Funktion,dieichin einemmeiner Themen verwende,umeinen Beitrag zuerhalten,der aufeinem bestimmten Slugbasiert:
function get_post_id( $slug, $post_type ) { $query = new WP_Query( array( 'name' => $slug, 'post_type' => $post_type ) ); $query->the_post(); return get_the_ID(); }
Dadurch wirdeine Abfragemithilfe der WP-APIerstellt,umeinen Beitrageinesbestimmten Typsmit einembestimmten Slug abzurufen,die regulären Schleifenfunktionenfür das Ergebnis auszuführen und die ID des Beitrags zurückzugeben.Sie könnenes auch verwenden,um dengesamten Beitrag zurückzugeben,indem Sie die Funktionein wenig ändern,aber das liegtbei Ihnen.
I tend to shy away from direct DB queries. Instead, I use the
WP_Query
object to parse things for me.This is, basically, a function I use in one of my themes to get a post based on a given slug:
function get_post_id( $slug, $post_type ) { $query = new WP_Query( array( 'name' => $slug, 'post_type' => $post_type ) ); $query->the_post(); return get_the_ID(); }
This will create a query using the WP API to fetch a post of a specific type with a given slug, will run the regular loop functions on the result, and return the ID of the post. You could also use it to return the entire post by modifying the function a bit, but that's up to you.
-
Anstelle von "$ query->the_post ()",das die Schleife ändert und alle "get_the_xxx" -Funktionen danach stört,verwenden Sie lieber: if ($ query-> have_posts ()) { $ query->posts [0] -> ID zurückgeben; }} sonst { return 0; }}Instead of `$query->the_post()` which alters the loop and will disrupt any `get_the_xxx` functions after it, rather use: if ($query->have_posts()) { return $query->posts[0]->ID; } else { return 0; }
- 0
- 2015-12-14
- Mikepote
-
- 2017-07-29
Diesistjetztein paar Jahre alt,aberes wirdimmernoch angezeigt,wenn Siebei Google danach suchen.Hieristeine einfache Möglichkeit:
$page = get_page_by_title( 'my post title' );
oder
$page = get_post_ID_by_title('my post title', OBJECT, 'customposttype');
Verwenden Sie danneinfach das $page-Objekt,um die ID abzurufen:
$page->ID
This is a few years old now, but it still pops up when searching for this on Google. So, here's a simple way to do it:
$page = get_page_by_title( 'my post title' );
or
$page = get_post_ID_by_title('my post title', OBJECT, 'customposttype');
Then just use the $page object to grab the ID:
$page->ID
-
- 2018-02-03
Derzeit (WP> 4.9.2)finden Sie Ihren "beliebigen" Beitrag/Ihre Seite usw.nach Titel/Slugfolgendermaßen:
$page = get_posts( array( //'name' => 'your-post-slug', // by post slug 'title' => 'Your post title', // by post title 'post_type' => 'page' // post type of your preference ) ); if ($page = $page[0]) // First/lowest ID taken if many objects { // Then you do whatever is needed... // $id = $page->id; // $content = $page->post_content; // or $content = apply_filters('the_content', $page->post_content); // etc. }
Currently (WP >4.9.2) you'll find your "any" given post/page etc. by title/slug this way:
$page = get_posts( array( //'name' => 'your-post-slug', // by post slug 'title' => 'Your post title', // by post title 'post_type' => 'page' // post type of your preference ) ); if ($page = $page[0]) // First/lowest ID taken if many objects { // Then you do whatever is needed... // $id = $page->id; // $content = $page->post_content; // or $content = apply_filters('the_content', $page->post_content); // etc. }
Ich habe den Codex durchsucht,get_page_by_title ()nicht zum Laufengebracht undbin ziemlich überrascht,dassesfür diese Aufgabe keine Standard-WP-Funktion zugeben scheint.
Ichmuss die IDeinesbestimmten Posts/Cpt odereiner Seiteentwedermit dem Slug des Post-/Seitentitelsermitteln.Idealerweise sucheich Folgendes:
get_post_ID_by_title('My post title', 'customposttype');
Was sollichtun?