Skip to content

Instantly share code, notes, and snippets.

@everaldomatias
Last active February 20, 2024 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everaldomatias/fc9dc9d0b775f94a9ae98f10de1bd429 to your computer and use it in GitHub Desktop.
Save everaldomatias/fc9dc9d0b775f94a9ae98f10de1bd429 to your computer and use it in GitHub Desktop.
Get WordPress post by name
<?php
if ( ! function_exists( 'get_post_by_name' ) ) {
function get_post_by_name($name, $post_type = 'post') {
$get_posts = get_posts(
[
'post_status' => 'publish',
'post_type' => $post_type,
'posts_per_page' => 1,
'name' => $name
]
);
if ( ! empty( $get_posts ) ) {
return $get_posts[0];
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment