Skip to content

[54461]: Query block toggle - get all descendants of current page#69053

Open
PaulREnglish wants to merge 7 commits into
WordPress:trunkfrom
PaulREnglish:feature/54461-query-block-parent-child-toggles
Open

[54461]: Query block toggle - get all descendants of current page#69053
PaulREnglish wants to merge 7 commits into
WordPress:trunkfrom
PaulREnglish:feature/54461-query-block-parent-child-toggles

Conversation

@PaulREnglish

@PaulREnglish PaulREnglish commented Feb 5, 2025

Copy link
Copy Markdown
Contributor

What?

Resolves: #54461

Adds a toggle to set the current page as the parent page.

Why?

See #54461 (comment).

How?

Switching the 'Set parent as current page' toggle to the 'on' position will set the Parents query to an array containing only the current page's id. Switching this toggle off will empty this array.

Testing Instructions

  1. Open a page (or any other hierarchal post type) in the editor
  2. Insert a query block
  3. Open the filters dropdown (click the 3 dots next to 'Filters' in the sidebar) and insert a 'Parents' filter
  4. Click 'Set parent as current page' to set the current page as the only parent.
  5. Child pages of the current page should be displayed in the query block
  6. Click save and go to front end
  7. Child pages of the current page should be displayed in the query block on the frontend

Testing Instructions for Keyboard

  1. In the page editor, insert a query block
  2. Using the tab keys navigate to the 'Set parent as current page' toggle
  3. Use the space bar to toggle

Screenshots or screencast

Before After
Screenshot on 2025-02-20 at 14-52-11 Screenshot on 2025-02-20 at 14-51-23

@github-actions

github-actions Bot commented Feb 5, 2025

Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @PaulREnglish! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Feb 5, 2025
@PaulREnglish PaulREnglish changed the title Feature/54461 query block parent child toggles [54461]: Query block toggle - get all descendants of current page Feb 5, 2025
@PaulREnglish

Copy link
Copy Markdown
Contributor Author

Here's a demo of how it currently works. Still a WIP but any feedback would be appreciated:

Screen.Capture.on.2025-02-05.at.15-41-44.mp4

@Mamaduka Mamaduka added [Type] Enhancement A suggestion for improvement. [Block] Query Loop Affects the Query Loop Block labels Feb 5, 2025
@Drivingralle

Copy link
Copy Markdown
Contributor

What do you think about add the setting to the existing input field like this?

408529130-6f5f9552-3c72-42eb-b919-b2677ef25cc6

@PaulREnglish

PaulREnglish commented Feb 6, 2025

Copy link
Copy Markdown
Contributor Author

What do you think about add the setting to the existing input field like this?

408529130-6f5f9552-3c72-42eb-b919-b2677ef25cc6

Hi 👋! Thanks for the question :D
During initial implementation, I had made the toggle set the existing text/input field to contain just the current page without hiding it like you suggested. However, the second toggle (the "Show all descendants one") will only work if we have exactly one parent (as get_page_children accepts one page id). So to avoid editors using the "show all descendants" toggle on multiple parents, I hid the text/input field when that toggle is visible. Hence, the current behaviour shown in my demo.

@PaulREnglish

Copy link
Copy Markdown
Contributor Author

Does anyone have any thoughts regarding the approach I've taken codewise with regards to getting all descendants?
Currently the query fetches all pages from the database before filtering them with the use of get_page_children. This has required the use of 3 filters (query_loop_block_query_vars, post_limits_request and posts_results) and the use of a new post_ancestor attribute.

An alternative approach I did consider but haven't used would be to loop over the page hierarchy and execute a different sql query for every depth we need to fetch children from. (So one sql query would fetch the direct children of the parent, a second sql query would fetch the children of those children, etc, etc).

@PaulREnglish

Copy link
Copy Markdown
Contributor Author

After some discussion in Slack, I'm going to try to add the post_ancestor query property to WordPress core so I don't have to use the 'hacky' approach of using filters.

See ticket here: https://core.trac.wordpress.org/ticket/62953#ticket

@Mamaduka

Copy link
Copy Markdown
Member

Thanks again for working on this, @PaulREnglish!

Let's continue the technical discussion around post_ancestor in the core ticket. Once that's handled, the UI can be updated for the Query block.

I'm going to remove this enhancement request from the WP 6.8 project board.

@PaulREnglish

Copy link
Copy Markdown
Contributor Author

Thanks again for working on this, @PaulREnglish!

My pleasure!

Let's continue the technical discussion around post_ancestor in the core ticket. Once that's handled, the UI can be updated for the Query block.

I'm going to remove this enhancement request from the WP 6.8 project board.

Sounds good.

@joemcgill

Copy link
Copy Markdown
Member

@PaulREnglish raised this PR and the related Trac ticket in today's Dev Chat. While I agree that adding core support for querying all ancestors will require some discussion that will cause this to miss the 6.8 milestone, I think it might be worth reducing the scope of this PR to only address the original issue, which is to set the query block to show only direct children of the current page.

I think that could be done with existing functionality and then iterated on in future versions of GB that would be included in 6.9.

The usage of filters was a rather hacky implementation as the ancestor query prop ought to be implemented in WordPress Core rather than Gutenberg.
@PaulREnglish PaulREnglish force-pushed the feature/54461-query-block-parent-child-toggles branch from 0016873 to ab0ab27 Compare February 20, 2025 14:33
@PaulREnglish

PaulREnglish commented Feb 20, 2025

Copy link
Copy Markdown
Contributor Author

@PaulREnglish raised this PR and the related Trac ticket in today's Dev Chat. While I agree that adding core support for querying all ancestors will require some discussion that will cause this to miss the 6.8 milestone, I think it might be worth reducing the scope of this PR to only address the original issue, which is to set the query block to show only direct children of the current page.

I think that could be done with existing functionality and then iterated on in future versions of GB that would be included in 6.9.

Thanks very much for your input! I've reverted the ancestor/show all descendants toggle leaving behind only the "Set parent as current page" toggle which should hopefully address the original issue.

I'll take some screenshots of how it works then I can mark this PR as ready to review.

@PaulREnglish PaulREnglish marked this pull request as ready for review February 20, 2025 15:54
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: PaulREnglish <bbpaule@git.wordpress.org>
Co-authored-by: Drivingralle <drivingralle@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: joemcgill <joemcgill@git.wordpress.org>
Co-authored-by: cagrimmett <cagrimmett@git.wordpress.org>
Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org>
Co-authored-by: webmandesign <webmandesign@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Query Loop Affects the Query Loop Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query Loop block: Allow creating loop to display child pages

4 participants