Support

Account

Home Forums General Issues get_field for a menu fails

Solved

get_field for a menu fails

  • I’ve followed the tutorial on how to add custom fields to menus. The field setup works, the field is displayed in the menu and saved correctly to the wp_termmeta table when content is added to the field from the menu edit interface.

    In my functions.php, following the above linked tutorial, any calls to retrieve the field values, however, fail and return NULL, i.e. this outputs NULL:

    $logo = get_field('logo', $args->menu);
    var_dump($logo);

    $args->menu returns the display name of my menu, so I am wondering how that code even is supposed to work, when get_field() should be passed a POST_ID. The tutorial points out:

    WP stores each Menu as a term object in the wp_terms table. ACF will store all custom field values in the wp_termmeta table.

    By why are my get_field() calls not returning anything? I’ve also tried getting the term_id of the menu and passed that in, as well as (for sake of testing) manually tried the menu’s string, slug or ID. The fields I’ve defined were an image, and later a simple text string. Both show fine in the wp_termsmeta table as the linked post for the image and the actual string for the text string in the “meta_value” column.

  • I think you may be something else incorrect, but I can’t tell what it is from what you’ve provided.

    When using this

    
    add_filter('wp_nav_menu_items', 'my_wp_nav_menu_items', 10, 2);
    
    function my_wp_nav_menu_items( $items, $args ) {
    
    ...
    

    $args->menu should be a term object. When I add this filter and do print_r($args) I get this:

    
    stdClass Object
    (
      [menu] => WP_Term Object
        (
          [term_id] => 30
          [name] => manue 1
          [slug] => manue-1
          [term_group] => 0
          [term_taxonomy_id] => 30
          [taxonomy] => nav_menu
          [description] => 
          [parent] => 0
          [count] => 7
          [filter] => raw
        )
    
      [container] => div
      [container_class] => 
      [container_id] => 
      [menu_class] => nav-menu
      [menu_id] => primary-menu
      [echo] => 1
      [fallback_cb] => wp_page_menu
      [before] => 
      [after] => 
      [link_before] => 
      [link_after] => 
      [items_wrap] => <ul id="%1$s" class="%2$s">%3$s</ul>
      [item_spacing] => preserve
      [depth] => 0
      [walker] => 
      [theme_location] => primary
    )
    

    Looking at this https://www.advancedcustomfields.com/resources/adding-fields-menu-items/

    
    add_filter('wp_nav_menu_objects', 'my_wp_nav_menu_objects', 10, 2);
    
    function my_wp_nav_menu_objects( $items, $args ) {
    
    ...
    

    $args results in the same.

    Probably a little known fact, but $post_id when calling ACF functions can also be an object of type post, term or user.

  • Okay,

    I played around some more, and this seems to be a bug/inconsistency related to the ambiguous ways you can call wp_nav_menu, in particular the menu attribute you can pass in, which can be:

    (int|string|WP_Term) Desired menu. Accepts (matching in order) id, slug, name, menu object.

    Here the different output for different ways of calling my menu:
    wp_nav_menu(array('menu' => 'Footer menu'));
    results in print_r($args->menu):

    stdClass Object ( [menu] => Footer menu [container] => div [container_class] => [container_id] => [menu_class] => menu [menu_id] => [echo] => 1 [fallback_cb] => wp_page_menu [before] => [after] => [link_before] => [link_after] => [items_wrap] =>
    %3$s
    [item_spacing] => preserve [depth] => 0 [walker] => [theme_location] => )

    Where as this:
    wp_nav_menu(array('menu' => 1));
    results in print_r($args->menu):

    stdClass Object ( [menu] => WP_Term Object ( [term_id] => 3 [name] => Footer menu [slug] => footer-menu [term_group] => 0 [term_taxonomy_id] => 3 [taxonomy] => nav_menu [description] => [parent] => 0 [count] => 1 [filter] => raw ) [container] => div [container_class] => [container_id] => [menu_class] => menu [menu_id] => [echo] => 1 [fallback_cb] => wp_page_menu [before] => [after] => [link_before] => [link_after] => [items_wrap] =>
    %3$s
    [item_spacing] => preserve [depth] => 0 [walker] => [theme_location] => ) MENUWP_Term Object ( [term_id] => 3 [name] => Footer menu [slug] => footer-menu [term_group] => 0 [term_taxonomy_id] => 3 [taxonomy] => nav_menu [description] => [parent] => 0 [count] => 1 [filter] => raw )

    So apparently the menu will render with both methods, but the $args->menu differs significantly, and in the string parameter variant does not result in the term object ACF is expecting. Hurray WordPress.

  • Ah, so if you’re passing the menu id, slug or name then you need to get the menu object yourself in your filter. https://codex.wordpress.org/Function_Reference/wp_get_nav_menu_object. WP is just passing back exactly what you pass in for $args. My testing was done on a basic theme and the menu is called using wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'menu_id' => 'primary-menu' ) );

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.