0

function c2_get_post_id_by_title( string $title = '' ): int {
    $posts = get_posts(
        array(
            'post_type'              => 'home-builder',
            'title'                  => $title,
            'numberposts'            => 1,
            'update_post_term_cache' => false,
            'update_post_meta_cache' => false,
            'orderby'                => 'post_date ID',
            'order'                  => 'ASC',
            'fields'                 => 'ids'
        )
    );
     return empty( $posts ) ? get_the_ID() : $posts[0];
}

Use like below

c2_get_post_id_by_title('Post Title');

Jagdish Sarma Asked question February 29, 2024
Add a Comment