admin管理员组文章数量:1289635
I have a list of properties that have been created as a custom post type. There are multiple queries that show these posts in different areas of the site.
I'm trying to add the ability to restrict certain posts from appearing for anyone except an approved user, while also keeping pagination, counts, etc, working as expected. I'm trying to modify the queries to achieve this.
To start, I added two new custom fields to the properties. One is to specify whether that post is restricted, which is a 1 or 0 to indicate yes or no. The second custom field indicates which user(s) are approved to view it, this field can contain multiple values, each in a separate row.
To help debug I'm outputting the WordPress query arguments to a text file while I work on modifying a single function.
When I output the original query from this function this is what I get:
Array
(
[post_type] => property
[posts_per_page] => 25
[paged] => 0
[post_status] => publish
[meta_query] => Array
(
[relation] => AND
[0] => Array
(
[0] => Array
(
[key] => fave_property_map_address
[compare] => EXISTS
)
[1] => Array
(
[key] => fave_property_price
[value] => Array
(
[0] => 100
[1] => 2500
)
[type] => NUMERIC
[compare] => BETWEEN
)
)
)
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => property_status
[field] => slug
[terms] => available
)
[relation] => AND
)
[orderby] => date
[order] => DESC
)
This query works perfectly, and returns results almost instantly.
When I modify that query to try to achieve what I'm doing, this is what I get:
Array
(
[post_type] => property
[posts_per_page] => 25
[paged] => 0
[post_status] => publish
[meta_query] => Array
(
[relation] => AND
[0] => Array
(
[0] => Array
(
[key] => fave_property_map_address
[compare] => EXISTS
)
[1] => Array
(
[key] => fave_property_price
[value] => Array
(
[0] => 100
[1] => 2500
)
[type] => NUMERIC
[compare] => BETWEEN
)
)
[1] => Array
(
[relation] => OR
[0] => Array
(
[relation] => AND
[0] => Array
(
[key] => restricted_access
[value] => 1
[compare] => =
)
[1] => Array
(
[key] => allowed_user
[value] => 1 // User ID of currently logged-in user
[compare] => =
)
)
[1] => Array
(
[relation] => OR
[0] => Array
(
[key] => restricted_access
[value] => 0
[compare] => =
)
[1] => Array
(
[key] => restricted_access
[compare] => NOT EXISTS
)
)
)
)
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => property_status
[field] => slug
[terms] => available
)
[relation] => AND
)
[orderby] => date
[order] => DESC
)
This code doesn't return any results and takes a really long time to actually complete the request according to my browser's developer tools.
The original query is already an "AND" query, and all I'm doing is adding an additional block to that, with some nested conditions.
So in addition to the original query conditions, I'm trying to add the following logic:
- Original Query AND
- IF "restricted_access" equals 0, or doesn't exist at all, show the post OR
- If "restricted_access" equals "1" and "allowed_user" equals the current user's ID, show the post.
This query takes over a minute to run and doesn't return the expected results. There is a test property that is set as restricted, and the allowed user ID is not mine, but the post still shows.
I think this is a two part problem. Why are the returned results not as expected, and why does this query take so long to run? There are less than 60 properties in the database.
The arguments in my second example above look like they should return the results expected. Any ideas or suggestions?
本文标签: phpLimiting posts based on nested metaquery
版权声明:本文标题:php - Limiting posts based on nested meta_query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741460725a2380021.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论