Obtener ID de publicación de WordPress desde el título del mensaje


Tengo un problema con un tema de WordPress personalizado que estoy desarrollando. Es un poco complicado, pero esencialmente, lo que necesito hacer es obtener un Id de Publicación por su Título de publicación. En pseudo-código sería idealmente algo como:

title = "foo";
post_id = get_post_id_where_title_is(title);

El título mencionado es una referencia estática que no se extrae de WordPress, ya está presente en la página.

Gracias de antemano.

Author: Aaron, 2009-10-08

9 answers

Solo una nota rápida para cualquiera que tropiece con esto:
get_page_by_title () ahora puede manejar cualquier tipo de post.
El parámetro $post_type se ha añadido en WP 3.0.

 85
Author: Michal Mau,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-01 09:26:31

Encontró una solución si alguien más lucha con esto. Solo publicado la pregunta de la desesperación después de 4 horas de prueba / Googlear!

function get_post_by_title($page_title, $output = OBJECT) {
    global $wpdb;
        $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='post'", $page_title ));
        if ( $post )
            return get_post($post, $output);

    return null;
}

Se encuentra en: http://sudarmuthu.com/blog/2009/09/18/retrieving-posts-and-pages-based-on-title-in-wordpress.html

 19
Author: Aaron,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-08 10:01:40

Como Michal Mau mencionó:

Use

$my_post = get_page_by_title( 'My Title', OBJECT, 'post' );
echo $my_post->post_content;

Es ( $page_title, $output, $post_type ) recibir fácilmente un post en lugar de una página.

 7
Author: BrainBUG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-27 13:18:51

Puede que esto le ayude más al crear la función para que no necesite repetir el código

function get_page_id_by_title($title)
{
$page = get_page_by_title($title);
return $page->ID;
}

$title = "your title";
get_page_id_by_title($title);
 2
Author: Kundan SIngh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-09-15 07:56:50

Puede usar el siguiente código según [un enlace] [http://codex.wordpress.org/Function_Reference/get_page_by_title]1 )!

<?php 
$page = get_page_by_title( 'About' );
wp_list_pages( 'exclude=' . $page->ID );
?>
 1
Author: Ramkumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-06 05:35:39

Otra forma de obtener el post y el ID de la página, es usar un plugin..

Hay un plugin, que lo que simplemente hace, es simplemente agregar una columna a todas sus páginas, todos los mensajes, todas las tablas de categorías, y tener un título de columna de ID...y justo debajo, verá toda la id de página / publicación listada en esa columna..

Creo que eso debería ser muy útil..

Uso este plugin con mucha frecuencia y es muy ligero.

Http://getyourblogready.com/?p=758

 0
Author: user1470261,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-20 19:21:50

No es necesario utilizar ningún tipo de querys SQL o plugin, utilice las funciones estándar de Wordpress para esto

$page = get_page_by_title( 'Home' );
$page_id = $page->ID;
 0
Author: Erik Larsson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-12 08:24:45

Es fácil obtener el id de post del título del post usando wp query:

global $wpdb;

$rw = $wpdb->get_row( $wpdb->prepare("select * from "your post table name" where post_title='your variable name or your post title'"));

echo $rw->ID;
 0
Author: Omdev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-06 12:57:25

1) difieren post_title y post_name el uno del otro. post_name tal vez es la babosa. post_title es el título de post.

2)

$titlee = "yourtitle";
echo $id = $wpdb->get_var("SELECT ID FROM $GLOBALS['wpdb']->posts WHERE post_name = $titlee");
 0
Author: T.Todua,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-16 08:36:15