Fix get_default_page_id_by_type #205
Open
+30
−28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
Recently we added a feature which should pre-select page for the page type in the "Your Pages" section on the Settings page. It works by searching through existing pages' titles, any found matches for specific page type are compared to matches found for other page types and any overlapping results are excluded. What is left is returned as a suggestion.
There is a bug there, which @tacoverdo described in #201 and I believe it was introduced when then feature was refactored.
I think that $posts here shouldn't be always set to
contact pages
andreturn empty( $posts ) ? 0 : $posts[0];
(on line 332) will execute after the first loop iteration (which means other page types wont be checked).I refactored
get_default_page_id_by_type
to be simpler and to me it looks like it does the same thing, but I will need another pair of eyes since I didn't implement this and I might be wrong.Also I changed homepage subarray to be just home page ID, instead of the WP_Post object, to be consistent with the others. As far I saw it doesnt have to be an object and it might be set to it because in initial implementation Post objects were used for other subarrays as well (but then we changed it so
get_posts_by_title
method returns just IDs to be lighter).Summary
This PR can be summarized in the following changelog entry:
Fixes suggested pages for "Your pages" section on Settings page.
Quality assurance
Fixes #201