Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation Page Link - Searches posts instead of pages #29396

Closed
Addison-Stavlo opened this issue Feb 27, 2021 · 4 comments · Fixed by #29993
Closed

Navigation Page Link - Searches posts instead of pages #29396

Addison-Stavlo opened this issue Feb 27, 2021 · 4 comments · Fixed by #29993
Assignees
Labels
[Block] Navigation Affects the Navigation Block [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended

Comments

@Addison-Stavlo
Copy link
Contributor

May be this is the cause of #29395 as well.

When using the navigation "Page Link" feature to search for pages, the results given seem to be for posts instead.

Here is the starting suggestion before any input (Suggests my post "hello world" instead of my page "sample page"):
Screen Shot 2021-02-26 at 8 02 32 PM

When searching for my page, it finds no results even if published:
Screen Shot 2021-02-26 at 8 02 46 PM

@paaljoachim
Copy link
Contributor

It sounds like a bug to me....

@Addison-Stavlo Addison-Stavlo added the [Type] Bug An existing feature does not function as intended label Mar 1, 2021
@Psmith-Ukridge
Copy link

I'm just getting started working with blocks in a serious way - started using Hensen theme and had this same problem - only posts come up when you try to add new pages to the navigation. I'm used to the menus option in wordpress - that really threw me and I wasted a lot of time thinking I was doing something wrong.

@kjellr
Copy link
Contributor

kjellr commented Mar 18, 2021

Running trunk, I do see pages returned in the post/page editor, but I do not see them returned in the Site Editor.

(Also, this may be somehow related to #29984)

@gwwar
Copy link
Contributor

gwwar commented Mar 18, 2021

Looks like this one probably happened since site editor was created:

Basically we have multiple implementations of fetchLinkSuggestions and they're getting out of sync.

Post-Editor:

const fetchLinkSuggestions = async (
search,
{ isInitialSuggestions, type, subtype, page, perPage: perPageArg } = {},
{ disablePostFormats = false } = {}
) => {
const perPage = perPageArg || isInitialSuggestions ? 3 : 20;
const queries = [];
if ( ! type || type === 'post' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'post',
subtype,
} ),
} ).catch( () => [] ) // fail by returning no results
);
}
if ( ! type || type === 'term' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'term',
subtype,
} ),
} ).catch( () => [] )
);
}
if ( ! disablePostFormats && ( ! type || type === 'post-format' ) ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
page,
per_page: perPage,
type: 'post-format',
subtype,
} ),
} ).catch( () => [] )
);
}
return Promise.all( queries ).then( ( results ) => {
return map(
flatten( results )
.filter( ( result ) => !! result.id )
.slice( 0, perPage ),
( result ) => ( {
id: result.id,
url: result.url,
title: decodeEntities( result.title ) || __( '(no title)' ),
type: result.subtype || result.type,
} )
);
} );
};

Site Editor:

const fetchLinkSuggestions = ( search, { perPage = 20 } = {} ) =>
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
per_page: perPage,
search,
type: 'post',
subtype: 'post',
} ),
} )
.then( ( posts ) =>
Promise.all(
posts.map( ( post ) =>
apiFetch( { url: post._links.self[ 0 ].href } )
)
)
)
.then( ( posts ) =>
posts.map( ( post ) => ( {
url: post.link,
type: post.type,
id: post.id,
slug: post.slug,
title: post.title.rendered || __( '(no title)' ),
} ) )
);

Navigation Editor:

export default function fetchLinkSuggestions(
search,
{ isInitialSuggestions, type, subtype } = {},
{ disablePostFormats = false } = {}
) {
const perPage = isInitialSuggestions ? 3 : 20;
const linkTypes = [ 'post', 'term', 'post-format' ];
linkTypes.forEach( ( linkType ) => {
if ( ! type || type === linkType ) {
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'post',
subtype,
} ),
} ).catch( () => [] ); // fail by returning no results
}
} );
const queries = [];
if ( ! type || type === 'post' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'post',
subtype,
} ),
} ).catch( () => [] ) // fail by returning no results
);
}
if ( ! type || type === 'term' ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'term',
subtype,
} ),
} ).catch( () => [] )
);
}
if ( ! disablePostFormats && ( ! type || type === 'post-format' ) ) {
queries.push(
apiFetch( {
path: addQueryArgs( '/wp/v2/search', {
search,
per_page: perPage,
type: 'post-format',
subtype,
} ),
} ).catch( () => [] )
);
}
return Promise.all( queries ).then( ( results ) => {
return map( flatten( results ).slice( 0, perPage ), ( result ) => ( {
id: result.id,
url: result.url,
title: decodeEntities( result.title ) || __( '(no title)' ),
type: result.subtype || result.type,
} ) );
} );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended
Projects
None yet
5 participants