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

Query Loop Block: Set depth limit #68620

Open
Drivingralle opened this issue Jan 12, 2025 · 9 comments
Open

Query Loop Block: Set depth limit #68620

Drivingralle opened this issue Jan 12, 2025 · 9 comments
Labels
[Block] Query Loop Affects the Query Loop Block Needs Design Needs design efforts. [Type] Enhancement A suggestion for improvement.

Comments

@Drivingralle
Copy link

What problem does this address?

While setting up a Query-Loop block inside the footer to list posts from a CPT, the query is fetching post from all levels. I want to limit the query to just the top level.

If the posts would be children of a singular post I could use the parents field. But for top level post don't have a parent and there is no way to set the parents field to 0.
A custom WP_query in classic world can do this.
Also changing the query of the Query block via a php filter works.

What is your proposed solution?

Similar to my suggestion in #54461 it would be possible to give the parents field an option to set it to no-parent/top-level/0.

Alternatively and way more complicated:
Add an option to the filter panel to set the depth of the query to X levels deep.
This setting could be combined with the parents field. Fetching post at are children of "post-id" or grand-children X levels down.

@Drivingralle Drivingralle added the [Type] Enhancement A suggestion for improvement. label Jan 12, 2025
@Mamaduka Mamaduka added the [Block] Query Loop Affects the Query Loop Block label Jan 13, 2025
@Mamaduka
Copy link
Member

But for top level post don't have a parent and there is no way to set the parents field to 0.

It is not ideal, but you can do this by setting the "parents":[0] via Code Editor. But I agree there should be easier way to set this.

Add an option to the filter panel to set the depth of the query to X levels deep.
This setting could be combined with the parents field. Fetching post at are children of "post-id" or grand-children X levels down.

This might require REST API changes first. I don't remember if similar queries are supported.

@Drivingralle
Copy link
Author

Interesting idea to do it via the code view.

Sadly I seam not to work fully. Inside the site editor it looks good. But in the frontend it doesn't work. The query in frontend seam to have some extra validation.

Here in a video:
https://cloudup.com/cXMFW3Rb1r5

Outside the video I tried to filter by "Sample page". That works.

@Mamaduka
Copy link
Member

I think it is due to this line in the build_query_vars_from_query_block method - https://github.com/WordPress/wordpress-develop/blob/d2a7bee9a6201f0276492ff29afd8c509480d266/src/wp-includes/blocks.php#L2594. The array_filter will remove all "empty" values, including 0, but it's valid value for parents.

@annezazu annezazu changed the title Query Loop Block: Set detph limit Query Loop Block: Set depth limit Jan 20, 2025
@Mamaduka Mamaduka moved this to 📥 Todo in WordPress 6.8 Editor Tasks Jan 23, 2025
@Mamaduka
Copy link
Member

@Drivingralle, create PR that should resolve "parents":[0] allowing programmatically creation top-level item queries - #68983.

The next step would be to provide a UI and allow users settings this value. Requesting feedback because I'm unsure what's the best solution here.

One option could be to have a static token, something like "No parent", which maps to 0.

Screenshot

Image

@Mamaduka Mamaduka added the Needs Design Needs design efforts. label Jan 31, 2025
@Mamaduka
Copy link
Member

Mamaduka commented Feb 4, 2025

@Drivingralle, the #68983 is merged now. You should be able to create similar queries programmatically.

@kjnanda
Copy link

kjnanda commented Feb 9, 2025

I tested this PR and looks like it does not take the argument that we set as value in Parent Filter. I followed the testing instruction shared in the PR but looks like it is not working.

@Mamaduka can you please share the visual of fix? 🙇‍♀

@Mamaduka
Copy link
Member

Mamaduka commented Feb 9, 2025

@kjnanda, #68983 only partially solves the issue by allowing programmatically setting "parents":[0], as noted in my previous comment and the PR; it doesn't provide the UI for it. The PR contains an example Query markup you could use for testing.

Regarding UI - The Parent filter will be updated once there's a good design proposal.

@kjnanda
Copy link

kjnanda commented Feb 9, 2025

Okay, got it!

Thank you for the prompt response! 🙌🏻

@Mamaduka
Copy link
Member

Moving this out of the WP 6.8 project board. We could resume working on this enhancement once there's a design proposal. Meanwhile, consumers can programmatically set paret: 0.

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 Needs Design Needs design efforts. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

3 participants