Get custom meta from post_name, post_slug or post_title

Im developing a website for a company using wordpress
They have thousands and thousands of posts and pages.
The company wants to have custom fields where they put informations like opening hours and staff_info, and then use shortcodes to display the informations here and there on the website.

That way , they can just change this custom field , and that will replicate through the entire posts and pages.. Smart move.. Ok..

Read More

I came up with this function for a shortcode :

/* ### Staff list shortcode ### */
// [stafflist "name" post_id=14600] - this will get name field from post_id=14600
// [stafflist "phone" post_id=14600]
// [stafflist "email" post_id=14600]
add_shortcode( 'stafflist', 'custom_stafflist_shortcode' );
function custom_stafflist_shortcode( $atts ) {
    extract(shortcode_atts(array(
        'post_id' => NULL,
    ), $atts));
    if( !isset( $atts[0] ) ) 
        return;
    $stafflist = esc_attr( $atts[0] );
    global $post;
    $post_id = ( NULL === $post_id ) ? $post->ID : $post_id;
    return get_post_meta( $post_id, $stafflist, true );
}

I want to simplify this a bit more if its possible for them.

Is it possible to make this function get phone from post_slug or post_url or something else than post_id ?

The perfect solution would be if I could use something like [stafflist “phone” title=CEO] or [stafflist “phone” post_slug=CEO] to display the phone number for the ceo.

Like I said, I want this to be as simple as possible for them.
Is this possible ?

If so , can anyone help out ?

Thanks

Related posts