Get post ID of current logged in user and add a link to the menu

旧时模样 提交于 2019-12-02 13:27:38

If I'm reading your question right... you want to be able to get all the posts authored by the currently logged in user? In your case, it will only ever be one post? If this is the case, you want something like this:

    global $current_user;    
    $args = array(
        'post_type'      => 'name of job custom post type',
        'author'         => $current_user->ID,
        'status'         => 'publish',
        'posts_per_page' => 1
        );
    $jobs = get_posts( $args );

You may or may not need the global $current_user, but it won't hurt. The above will return one published post of which the currently logged in user is an author of. Just put the appropriate custom post type name in, as I don't know what that plugin generates for a name.

UPDATE

To generate a link to this post and add it to the end of your current navigation, you would use something like:

function new_nav_menu_items( $items ) {
    global $current_user;    
    $args = array(
        'post_type'      => 'job_listing',
        'author'         => $current_user->ID,
        'status'         => 'publish',
        'posts_per_page' => 1
        );
    $jobs = get_posts( $args );
    $link = '<li><a href="' . get_permalink( $jobs->ID ) . '">Your Job</a></li>';
    // add link to the end of the menu
    $items = $items . $link;
    return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );

If you want the link somewhere special inside the menu, you'll need to resort to a custom menu walker.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!