Get post by post name instead of id

Asked
Active3 hr before
Viewed126 times

8 Answers

90%

Meta Stack Overflow ,Stack Overflow en español,As you can see to the above code, the routine is to get the post by ID, but my permalinks change into post name instead of post id for SEO purposes. How do I get the post by post name?,Stack Overflow em Português

If either of these are a problem for you, then you should consider just using the WP_Query class to get your post by name:

$found_post = null;

if ($posts = get_posts(array(
      'name' => 'my_post_slug',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1
   ))) $found_post = $posts[0];

// Now, we can do something with $found_post
if (!is_null($found_post)) {
   // do something with the post...
}
load more v
88%

Thanks for contributing an answer to WordPress Development Stack Exchange!,WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up., WordPress Development Meta , WordPress Development help chat

Use get_page_by_title(). It works with any post type.

$post = get_page_by_title($post_name, OBJECT, 'post');
echo $post - > ID;
72%

If more than one post uses the same title, the post with the smallest ID will be returned. Be careful: in case of more than one post having the same title, it will check the oldest publication date, not the smallest ID.,Because this function uses the MySQL ‘=’ comparison, $page_title will usually be matched as case-insensitive with default collation.,It is very important to mention that the function will disregard any post_status so if it finds and old post that is draft, it will show that result. So if you need to filter by post status you better build your own query.,This example will return the $page object for the page titled “About”. Then the $page->ID element is used to exclude the About page when listing pages.

function get_page_by_title($page_title, $output = OBJECT, $post_type = 'page') {
   global $wpdb;

   if (is_array($post_type)) {
      $post_type = esc_sql($post_type);
      $post_type_in_string = "'".implode("','", $post_type).
      "'";
      $sql = $wpdb - > prepare(
         "
         SELECT ID FROM $wpdb - > posts WHERE post_title = % s AND post_type IN($post_type_in_string)
         ",
         $page_title
      );
   } else {
      $sql = $wpdb - > prepare(
         "
         SELECT ID FROM $wpdb - > posts WHERE post_title = % s AND post_type = % s ",
         $page_title,
         $post_type
      );
   }

   $page = $wpdb - > get_var($sql);

   if ($page) {
      return get_post($page, $output);
   }
}
load more v
65%

$post_id = 5 // example post id
$post_content = get_post($post_id);
$content = $post_content - > post_content;
echo apply_filters('the_content', $content);
load more v
75%

The title mentioned is a static reference not being pulled in from WordPress, it's already present on the page.,it is easy to get the post id from post title using wp query:,1) differ post_title and post_name from each other. post_name maybe is the slug. post_title is the title of post.,I have an issue with a custom WordPress theme I'm developing. It's a bit convoluted, but essentially, what I need to do is get a Post Id by it's Post Title. In pseudo-code it would ideally be something like:

I have an issue with a custom WordPress theme I'm developing. It's a bit convoluted, but essentially, what I need to do is get a Post Id by it's Post Title. In pseudo-code it would ideally be something like:

title = "foo";
post_id = get_post_id_where_title_is(title);
load more v
40%

Introduction to the WordPress get_posts Function,Using WordPress get_posts is a two-step process:,An advanced list of posts built with the get_posts function,The Codex describes the get_posts function as follows:

We can use get_posts this way:

$args = array(
	'numberposts'	=> 20,
	'category'		=> 4
);
$my_posts = get_posts( $args );

if( ! empty( $my_posts ) ){
	$output = '<ul>';
	foreach ( $my_posts as $p ){
		$output .= '<li><a href="' . get_permalink( $p->ID ) . '">' 
		. $p->post_title . '</a></li>';
	}
	$output .= '<ul>';
}
load more v
22%

But get_the_title is the best method to get the title of the post.,So, get_the_title can receive the post id to return the title.,Although, the function can receive two optional parameters.,Are you looking for the WordPress function to retrieve a certain post title using the id?

load more v
60%

You often have come across instances where a plugin, code snippet, or WordPress itself has asked for a Post ID. Although they are not too hard to find, there are several, but simple ways to easily find Post ID on your WordPress site.,You can use the straight-forward method of finding Post IDs in WordPress Posts dashboard or simply install a free WordPress plugin.,If you are a developer or have experience working with PHP, there are some technical methods you can use to access Post IDs.,You can also use them while customizing codes to filter or exclude them using the_ID() function. Using a plugin like Search by ID, you can search for posts using Post IDs in the WordPress Posts dashboard.

For example, to hide certain pages from the sitemap, add the following code snippet to the functions.php file of your activated website theme.

add_filter('wpseo_exclude_from_sitemap_by_post_ids', function() {
   return array(311, 322);
});

Other "undefined-undefined" queries related to "Get post by post name instead of id"