Last active
February 20, 2024 17:51
-
-
Save everaldomatias/fc9dc9d0b775f94a9ae98f10de1bd429 to your computer and use it in GitHub Desktop.
Get WordPress post by name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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