|
1 | 1 | /**
|
2 | 2 | * WordPress dependencies
|
3 | 3 | */
|
4 |
| -import { store, getContext, getElement } from '@wordpress/interactivity'; |
| 4 | +import { |
| 5 | + store, |
| 6 | + getContext, |
| 7 | + getElement, |
| 8 | + getConfig, |
| 9 | +} from '@wordpress/interactivity'; |
5 | 10 |
|
6 | 11 | /** @type {( () => void ) | null} */
|
7 | 12 | let supersedePreviousSearch = null;
|
@@ -110,18 +115,31 @@ const { state, actions } = store(
|
110 | 115 | return;
|
111 | 116 | }
|
112 | 117 |
|
113 |
| - const url = new URL( window.location.href ); |
| 118 | + let url = new URL( window.location.href ); |
114 | 119 |
|
115 | 120 | if ( value ) {
|
116 |
| - // Set the instant-search parameter using the query ID and search value |
117 |
| - const queryId = ctx.queryId; |
118 |
| - url.searchParams.set( |
119 |
| - `instant-search-${ queryId }`, |
120 |
| - value |
121 |
| - ); |
| 121 | + if ( ctx.isInherited ) { |
| 122 | + // Get the canonical URL from the config |
| 123 | + const { canonicalURL } = getConfig( 'core/search' ); |
| 124 | + |
| 125 | + // Make sure we reset the pagination. |
| 126 | + url = new URL( canonicalURL ); |
| 127 | + url.searchParams.set( 'instant-search', value ); |
| 128 | + } else { |
| 129 | + // Set the instant-search parameter using the query ID and search value |
| 130 | + const queryId = ctx.queryId; |
| 131 | + url.searchParams.set( |
| 132 | + `instant-search-${ queryId }`, |
| 133 | + value |
| 134 | + ); |
122 | 135 |
|
123 |
| - // Make sure we reset the pagination. |
124 |
| - url.searchParams.set( `query-${ queryId }-page`, '1' ); |
| 136 | + // Make sure we reset the pagination. |
| 137 | + url.searchParams.set( `query-${ queryId }-page`, '1' ); |
| 138 | + } |
| 139 | + } else if ( ctx.isInherited ) { |
| 140 | + // Reset global search for inherited queries |
| 141 | + url.searchParams.delete( 'instant-search' ); |
| 142 | + url.searchParams.delete( 'paged' ); |
125 | 143 | } else {
|
126 | 144 | // Reset specific search for non-inherited queries
|
127 | 145 | url.searchParams.delete(
|
|
0 commit comments