Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#34947 closed enhancement (fixed)

Add filter to search available menu items query

Reported by: timothyblynjacobs's profile TimothyBlynJacobs Owned by: westonruter's profile westonruter
Milestone: 4.5 Priority: normal
Severity: normal Version:
Component: Customize Keywords: has-patch has-unit-tests commit
Focuses: Cc:


The customize_nav_menu_available_items filter in combination with the customize_nav_menu_available_item_types allow us to add custom menu items to the customizer. But there is no corresponding filter in the search method search_available_items_query. So when a user searches for menu items, we can't make ours appear.

Attachments (2)

34947.1.diff (643 bytes) - added by TimothyBlynJacobs 8 years ago.
34947.2.diff (4.1 KB) - added by westonruter 8 years ago.

Download all attachments as: .zip

Change History (11)

#1 @TimothyBlynJacobs
8 years ago

  • Keywords has-patch added

Patch added.

#2 @westonruter
8 years ago

@TimothyBlynJacobs: I think I actually came across this exact issue last week. I wanted to include private pages in my available items, so what I did (somewhat of a hack) is like:

add_action( 'pre_get_posts', function( \WP_Query $query ) {
        if ( ! doing_action( 'wp_ajax_load-available-menu-items-customizer' ) ) {
        $post_type = $query->get( 'post_type' );
        if ( ! $post_type ) {
        $post_type_obj = get_post_type_object( $post_type );
        if ( ! $post_type_obj ) {
        if ( current_user_can( $post_type_obj->cap->read_private_posts ) ) {
                $query->set( 'post_status', array_merge( array( 'private' ), (array) $query->get( 'post_status' ) ) );
} );

The same approach could be used to modify the query for searching, by looking to see if: doing_action( 'wp_ajax_search-available-menu-items-customizer' ).

Last edited 8 years ago by westonruter (previous) (diff)

#3 @TimothyBlynJacobs
8 years ago

@westonruter The problem is that the custom menu items we want to add aren't post objects. They are a custom menu type, so we can't retrieve them by modifying the query.

#4 @westonruter
8 years ago

  • Milestone changed from Awaiting Review to 4.5
  • Owner set to westonruter
  • Status changed from new to accepted

#5 @johnbillion
8 years ago

  • Version trunk deleted

#6 @westonruter
8 years ago

I'm going to rename this filter to customize_nav_menu_searched_items.

8 years ago

#7 @westonruter
8 years ago

  • Keywords has-unit-tests commit added

This ticket was mentioned in Slack in #core by jorbin. View the logs.

8 years ago

#9 @westonruter
8 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 36676:

Customize: Introduce customize_nav_menu_searched_items filter for modifying results of nav menu item searches.

This new filter can be used in conjunction with the customize_nav_menu_available_items and customize_nav_menu_available_item_types filters.

Props TimothyBlynJacobs, westonruter.
Fixes #34947.

Note: See TracTickets for help on using tickets.