Categories
Pro Tips

Menu Management in WordPress with Increased Search Results

Managing a WordPress website often involves organizing complex menus, especially for larger sites with numerous pages and posts. Krasen Slavov, a proficient WordPress developer, has provided a valuable solution for those who frequently use the Appearance > Menus section in the WordPress admin. His function increases the number of search results, making menu management significantly more efficient.

The Challenge of Limited Search Results

When you search for items to add to your menus in WordPress, you might find the default number of search results limiting. This can be particularly cumbersome if you’re dealing with a large number of pages or custom post types. You’re often required to perform multiple searches to find all the items you need, which can be time-consuming and frustrating.

The Solution: A Custom Function to Increase Search Results

To address this issue, Krasen Slavov devised a simple yet effective function that hooks into WordPress’s pre_get_posts action. This function, appearance_menu_search_results, increases the number of search results displayed in the Appearance > Menus section, allowing for easier and quicker menu construction.

Here’s a look at the function:

/**
 * Increase search results for Appearance > Menus > Search
 */
function appearance_menu_search_results( $query ) {
    if ( isset( $_POST['action'] ) && 'menu-quick-search' === $_POST['action'] && isset( $_POST['menu-settings-column-nonce'] ) ) {
        if ( is_a( $query->query_vars['walker'], 'Walker_Nav_Menu_Checklist' ) ) {
            $query->query_vars['posts_per_page'] =  99;
        }
    }

    return $query;
}

add_action( 'pre_get_posts', 'appearance_menu_search_results', 10, 2 );

How It Works

  • The function checks if the current query is for the menu quick search.
  • If it is, it modifies the posts_per_page query variable to 99 (or any number you prefer), increasing the number of search results displayed.
  • This change only affects searches in the Appearance > Menus section, thanks to the specific checks in place.

Implementing the Function

To use this function:

Continue learning with our widely loved post on WordPress Pagination with Custom Functionality

  1. Access Your Theme’s functions.php File: You can do this via FTP or through your WordPress dashboard under Appearance > Theme Editor.
  2. Insert the Snippet: Paste the provided function into your functions.php file.

Benefits

  • Efficiency: Find and add menu items more quickly, especially useful for sites with a large amount of content.
  • Convenience: Reduce the need for multiple searches when adding items to menus.
  • Customization: Adjust the number of search results to suit your specific needs.

This function is a testament to the flexibility of WordPress and how small tweaks can significantly improve the user experience for administrators and content managers. By increasing the number of search results in the menu editor, you can streamline the process of building and managing your site’s navigation.

Many found our in-depth article on The Power of the Search and Replace Pro for Elementor to be helpful as well.

Thanks to Krasen Slavov for sharing this useful snippet with the WordPress community!

For more on Advanced Tutorials, don't forget to explore our resource on Mastering WordPress Hooks: Unlocking the Power of Actions and Filters

2 replies on “Menu Management in WordPress with Increased Search Results”

Leave a Reply

Your email address will not be published. Required fields are marked *