Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

How to get a WordPress post by slug

function get_post_by_slug($slug){
    $posts = get_posts(array(
            'name' => $slug,
            'posts_per_page' => 1,
            'post_type' => 'product',
            'post_status' => 'publish'
    ));
    
    if(! $posts ) {
        throw new Exception("NoSuchPostBySpecifiedID");
    }

    return $post[0];
}
Source by arashmilani.com #
 
PREVIOUS NEXT
Tagged: #How #WordPress #post #slug
ADD COMMENT
Topic
Name
8+3 =