Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now
5 added 68 characters in body
source | link

UPDATE 7/25/2019:

I finally got around to digging through the code and trying to create a submenu item when saving I was having a new custom post type event. The problem I'm running into is that when I save an event, it creates a menu item that seems to be an orphan where menu-item-object and _menu_item_type both say custom for both instead of lvo_event andgetting the post_typewp_update_nav_menu_item respectivelycall to correctly record the nav menu item into the database. It getsI first had to hardcode the correct value for _menu_item_menu_item_parent$menu_id and I think it also getsto the correct value for _menu_item_object_idterm_id when I look throughof the posts and postmeta tables inprimary menu so it would stop recording the databasemenu item as an orphaned item. Then I think my problem is with the first variable inhad to add wp_update_nav_menu_item'menu-item-type', as $post->menu_idpost_type. I'm guessing, because it should be referencing thekept defaulting to term_idcustom fromand ignoring my primary menu or maybe the ID$post variables. Below is my code which is a combination of multiple answers I've found to do checks to make sure it only adds the post for my parent menu item... Any thoughts on what I'm doing wrong? Below is the function I created. I also attached a screenshotnew publish of the postmeta database entry that is createdan event in my custom post type.

add_action( 'save_post', 'prefix_update_menu', 10, 3 );
function prefix_update_menu($post_id, $post, $update) { 

  $parent_menu_id = false; 

  if ('lvo_event' !== $post->post_type) :
    return;
  endif;

  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) :
      return;
  endif;

  // Check permissions
  if ( 'page' == $post->post_type ) :
    if ( !current_user_can( 'edit_page', $post_id ) ) :
        return;
    endif;
  else :
    if ( !current_user_can( 'edit_post', $post_id ) ) :
        return;
    endif;
  endif;

  if ( false ===== $update ) :
    return;
  endif;    

  // New post only
  if ( $post->post_date !== $post->post_modified ) {
    return;
  } 


  // Get main menu items
  $main_menu_items = wp_get_nav_menu_items( 'primary' );
  if ( ! $main_menu_items ) {:
    return;
  }endif;

  foreach ($main_menu_items as $menu_items):
    if ("Season Events & Tickets" == $menu_items->title):
        $parent_menu_id = $menu_items->ID;
    endif;
  endforeach;

  if ( false !== $parent_menu_id ) {
      // If you want to create menu items          
      wp_update_nav_menu_item( $post->menu_id2, 0, array(
        'menu-item-title'    object-id' => $post->post_title>ID,
        'menu-item-object-id'object'    => $post->ID>post_type,
        'menu-item-object'type'      => $post->post_type'post_type',
        'menu-item-parent-id' => $parent_menu_id, // not sure if property is correct, please check it yourself
        'menu-item-status'    => 'publish'
      ));
  }

}

Livermore Valley Opera Database Image Menu

UPDATE 7/25/2019:

I finally got around to digging through the code and trying to create a submenu item when saving a new custom post type event. The problem I'm running into is that when I save an event, it creates a menu item that seems to be an orphan where menu-item-object and _menu_item_type both say custom for both instead of lvo_event and post_type respectively. It gets the correct value for _menu_item_menu_item_parent and I think it also gets the correct value for _menu_item_object_id when I look through the posts and postmeta tables in the database. I think my problem is with the first variable in wp_update_nav_menu_item, $post->menu_id. I'm guessing it should be referencing the term_id from my primary menu or maybe the ID of the post for my parent menu item... Any thoughts on what I'm doing wrong? Below is the function I created. I also attached a screenshot of the postmeta database entry that is created.

add_action( 'save_post', 'prefix_update_menu', 10, 3 );
function prefix_update_menu($post_id, $post, $update) {
  $parent_menu_id = false;
  if ( false === $update ) {
    return;
  }

  // Get main menu items
  $main_menu_items = wp_get_nav_menu_items( 'primary' );
  if ( ! $main_menu_items ) {
    return;
  }

  foreach ($main_menu_items as $menu_items):
    if ("Season Events & Tickets" == $menu_items->title):
        $parent_menu_id = $menu_items->ID;
    endif;
  endforeach;

  if ( false !== $parent_menu_id ) {
      // If you want to create menu items          
      wp_update_nav_menu_item( $post->menu_id, 0, array(
        'menu-item-title'     => $post->post_title,
        'menu-item-object-id' => $post->ID,
        'menu-item-object'    => $post->post_type,
        'menu-item-parent-id' => $parent_menu_id, // not sure if property is correct, please check it yourself
        'menu-item-status'    => 'publish'
      ));
  }

}

Livermore Valley Opera Database Image Menu

UPDATE 7/25/2019: I was having a problem getting the wp_update_nav_menu_item call to correctly record the nav menu item into the database. I first had to hardcode the $menu_id to the term_id of the primary menu so it would stop recording the menu item as an orphaned item. Then I had to add 'menu-item-type' as post_type, because it kept defaulting to custom and ignoring my $post variables. Below is my code which is a combination of multiple answers I've found to do checks to make sure it only adds the menu item on a new publish of an event in my custom post type.

add_action( 'save_post', 'prefix_update_menu', 10, 3 );
function prefix_update_menu($post_id, $post, $update) { 

  $parent_menu_id = false; 

  if ('lvo_event' !== $post->post_type) :
    return;
  endif;

  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) :
      return;
  endif;

  // Check permissions
  if ( 'page' == $post->post_type ) :
    if ( !current_user_can( 'edit_page', $post_id ) ) :
        return;
    endif;
  else :
    if ( !current_user_can( 'edit_post', $post_id ) ) :
        return;
    endif;
  endif;

  if ( false == $update ) :
    return;
  endif;    

  // New post only
  if ( $post->post_date !== $post->post_modified ) {
    return;
  } 


  // Get main menu items
  $main_menu_items = wp_get_nav_menu_items( 'primary' );
  if ( ! $main_menu_items ) :
    return;
  endif;

  foreach ($main_menu_items as $menu_items):
    if ("Season Events & Tickets" == $menu_items->title):
        $parent_menu_id = $menu_items->ID;
    endif;
  endforeach;

  if ( false !== $parent_menu_id ) {
      // If you want to create menu items          
      wp_update_nav_menu_item( 2, 0, array(
        'menu-item-object-id' => $post->ID,
        'menu-item-object'    => $post->post_type,
        'menu-item-type'      => 'post_type',
        'menu-item-parent-id' => $parent_menu_id, // not sure if property is correct, please check it yourself
        'menu-item-status'    => 'publish'
      ));
  }

}
4 added 292 characters in body
source | link

I finally got around to digging through the code and trying to create a submenu item when saving a new custom post type event. The problem I'm running into is that when I save an event, it creates a menu item that seems to be an orphan where menu-item-object and _menu_item_type both say custom for both instead of lvo_event and post_type respectively. It gets the correct value for _menu_item_menu_item_parent and I think it also gets the correct value for _menu_item_object_id when I look through the posts and postmeta tables in the database. I think my problem is with the first variable in wp_update_nav_menu_item, $post->menu_id. I'm guessing it should be referencing the term_id from my primary menu or maybe the ID of the post for my parent menu item... Any thoughts on what I'm doing wrong? Below is the function I created. I also attached a screenshot of the postmeta database entry that is created.

Livermore Valley Opera Database Image Menu

I finally got around to digging through the code and trying to create a submenu item when saving a new custom post type event. The problem I'm running into is that when I save an event, it creates a menu item that seems to be an orphan where menu-item-object and _menu_item_type both say custom for both instead of lvo_event and post_type respectively. It gets the correct value for _menu_item_menu_item_parent and I think it also gets the correct value for _menu_item_object_id when I look through the posts and postmeta tables in the database. I think my problem is with the first variable in wp_update_nav_menu_item, $post->menu_id. I'm guessing it should be referencing my primary menu or maybe the parent menu item... Any thoughts on what I'm doing wrong? Below is the function I created.

I finally got around to digging through the code and trying to create a submenu item when saving a new custom post type event. The problem I'm running into is that when I save an event, it creates a menu item that seems to be an orphan where menu-item-object and _menu_item_type both say custom for both instead of lvo_event and post_type respectively. It gets the correct value for _menu_item_menu_item_parent and I think it also gets the correct value for _menu_item_object_id when I look through the posts and postmeta tables in the database. I think my problem is with the first variable in wp_update_nav_menu_item, $post->menu_id. I'm guessing it should be referencing the term_id from my primary menu or maybe the ID of the post for my parent menu item... Any thoughts on what I'm doing wrong? Below is the function I created. I also attached a screenshot of the postmeta database entry that is created.

Livermore Valley Opera Database Image Menu

3 added 1947 characters in body
source | link

UPDATE 7/25/2019:

I finally got around to digging through the code and trying to create a submenu item when saving a new custom post type event. The problem I'm running into is that when I save an event, it creates a menu item that seems to be an orphan where menu-item-object and _menu_item_type both say custom for both instead of lvo_event and post_type respectively. It gets the correct value for _menu_item_menu_item_parent and I think it also gets the correct value for _menu_item_object_id when I look through the posts and postmeta tables in the database. I think my problem is with the first variable in wp_update_nav_menu_item, $post->menu_id. I'm guessing it should be referencing my primary menu or maybe the parent menu item... Any thoughts on what I'm doing wrong? Below is the function I created.

add_action( 'save_post', 'prefix_update_menu', 10, 3 );
function prefix_update_menu($post_id, $post, $update) {
  $parent_menu_id = false;
  if ( false === $update ) {
    return;
  }

  // Get main menu items
  $main_menu_items = wp_get_nav_menu_items( 'primary' );
  if ( ! $main_menu_items ) {
    return;
  }

  foreach ($main_menu_items as $menu_items):
    if ("Season Events & Tickets" == $menu_items->title):
        $parent_menu_id = $menu_items->ID;
    endif;
  endforeach;

  if ( false !== $parent_menu_id ) {
      // If you want to create menu items          
      wp_update_nav_menu_item( $post->menu_id, 0, array(
        'menu-item-title'     => $post->post_title,
        'menu-item-object-id' => $post->ID,
        'menu-item-object'    => $post->post_type,
        'menu-item-parent-id' => $parent_menu_id, // not sure if property is correct, please check it yourself
        'menu-item-status'    => 'publish'
      ));
  }

}

UPDATE 7/25/2019:

I finally got around to digging through the code and trying to create a submenu item when saving a new custom post type event. The problem I'm running into is that when I save an event, it creates a menu item that seems to be an orphan where menu-item-object and _menu_item_type both say custom for both instead of lvo_event and post_type respectively. It gets the correct value for _menu_item_menu_item_parent and I think it also gets the correct value for _menu_item_object_id when I look through the posts and postmeta tables in the database. I think my problem is with the first variable in wp_update_nav_menu_item, $post->menu_id. I'm guessing it should be referencing my primary menu or maybe the parent menu item... Any thoughts on what I'm doing wrong? Below is the function I created.

add_action( 'save_post', 'prefix_update_menu', 10, 3 );
function prefix_update_menu($post_id, $post, $update) {
  $parent_menu_id = false;
  if ( false === $update ) {
    return;
  }

  // Get main menu items
  $main_menu_items = wp_get_nav_menu_items( 'primary' );
  if ( ! $main_menu_items ) {
    return;
  }

  foreach ($main_menu_items as $menu_items):
    if ("Season Events & Tickets" == $menu_items->title):
        $parent_menu_id = $menu_items->ID;
    endif;
  endforeach;

  if ( false !== $parent_menu_id ) {
      // If you want to create menu items          
      wp_update_nav_menu_item( $post->menu_id, 0, array(
        'menu-item-title'     => $post->post_title,
        'menu-item-object-id' => $post->ID,
        'menu-item-object'    => $post->post_type,
        'menu-item-parent-id' => $parent_menu_id, // not sure if property is correct, please check it yourself
        'menu-item-status'    => 'publish'
      ));
  }

}
2 added 379 characters in body
source | link
1
source | link