0

I am using a plugin called wp job manager, which creates "jobs" as wordpress posts. I am limiting the number of jobs a user can have to one and would like to add a direct link to their job in the navigation menu.

I would also like to add a edit link, which is dynamically generated using the post id of the job. Seeing as though users will only have one post , I believe this is do-able.

Edit: If I can even just get a link to the single post of the current logged in user and add it to the menu, I believe I can add the edit link inside the post itself.

The edit job url is created dynamiclly like this (in a loop)

 <?php foreach ( $jobs as $job ) : ?>
<tr>
   <td class="actions">
      <ul class="job-dashboard-actions">
      <?php
      $actions = array();
      switch ( $job->post_status ) {
      case 'publish' :
      $actions['edit'] = array( 'label' => __( 'Edit', 'wp-job-manager' ), 'nonce' => false );
      break;
      }
      $actions           = apply_filters( 'job_manager_my_job_actions', $actions, $job );
      foreach ( $actions as $action => $value ) {
      $action_url = add_query_arg( array( 'action' => $action, 'job_id' => $job->ID, ) , 'http://www.mywebsite./edit-my-profile/' );
      if ( $value['nonce'] ) {
      $action_url = wp_nonce_url( $action_url, 'job_manager_my_job_actions' );
      }
      echo '
      <li><a  href="' . esc_url( $action_url ) . '" class="job-dashboard-action-' . esc_attr( $action ) . '">' . esc_html( $value['label'] ) . '</a></li>
      ';
      }

THis generated a link such as this http://www.mywebsite.com/edit-my-profile/?action=edit&job_id=ID so if have tried getting the post id of the current author and appending it to this url and then adding it to the menu, but I can't seem to find a way to get the post id of current logged in user outside of the post loop.

So instead I tried using the global $actionurl variable and adding it to the menu with this function, but it just breaks the menu.

function username_nav($action_url) {
    return $menu = str_replace('editprofile', $action_url, $menu);
}

 add_filter( 'wp_nav_menu', 'username_nav' );

Any suggestions? Thanks in advance!

Itamar
  • 151
  • 3
  • 15
  • What does `` get you? https://codex.wordpress.org/Function_Reference/get_the_author and maybe this for editing the post https://codex.wordpress.org/Function_Reference/edit_post_link – Steve Jan 08 '16 at 20:38
  • Or to get the current user ID `` https://codex.wordpress.org/Function_Reference/get_current_user_id – Steve Jan 08 '16 at 20:44
  • @Steve Thanks, but I don't need the author id, I need to get the id of the post which the current logged in user is an author of. The current_user_id will help in this, but retrieving the post id outside the loop is the difficult part. – Itamar Jan 08 '16 at 20:54
  • Sorry - looks like Daniel C has got the right way then. +1 for him. – Steve Jan 08 '16 at 21:31
  • If you can get the ID inside the loop and know when you have got it, you could add it to a session variable at that point and use it later. Pseudocode: `if(found the id you want) $_SESSION['id_you_want'] = $job_ID;` Sessions are active in WordPress already. – Steve Jan 09 '16 at 04:24

1 Answers1

1

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.

Daniel C
  • 2,105
  • 12
  • 18
  • Thanks, but if I wanted to get the id or link to the this post would I use $id= $jobs -> ID; then get_post_permalink ($id)? And the custom post type name is "job_listing". – Itamar Jan 08 '16 at 21:36
  • I updated the code to reflect how you could add a link to the end of your current navigation... if you want it at the beginning, you can swap the $items = line... if you want it somewhere special, you'll need to do a custom walker, or more advanced coding. – Daniel C Jan 08 '16 at 22:02
  • Thanks, I am trying to get the id or the link to that post returned by get_posts(), or both hopefully. In the meantime I have tried using this, but it breaks the menu and it doesn't display ` global $current_user; $args = array( 'post_type' => 'job_listing', 'author' => $current_user->ID, 'status' => 'publish', 'posts_per_page' => 1 ); $jobs = get_posts( $args ); $action_url = get_permalink($jobs);` and to get the id `$jobids= wp_list_pluck( $jobs, 'ID' ); ` – Itamar Jan 08 '16 at 22:03
  • Unfortunately, the code above does not work. It returns me to the same page I am on, which leads me to believe that it is not retrieving the ids of the post. – Itamar Jan 08 '16 at 23:26
  • Do a `var_dump( $jobs )` after you call get_posts() and see what you're getting. Also do a `var_dump( $current_user )` to make sure you are getting the right info there as well. Change the variables accordingly to match what you need. This code will work, based on what you've described. If it's not getting the appropriate post, then I'm missing what you are trying to accomplish. – Daniel C Jan 11 '16 at 20:05