wordpress/mysql 构建导航菜单的问题
我正在开发一个 WordPress 主题,需要选择多个类别中的帖子来构建菜单。我可以将其设置为使用标签或标签和类别的组合来工作,但最终我需要能够通过多个猫/标签进行搜索。我能够找到的唯一允许在 cat/tag 上进行多重 AND 搜索的内置 WordPress 函数是 query_posts 函数,如下所示:
query_posts(array('category__and' => array(list_of_cats)))
有两个问题。首先是 wp 文档说不要使用它。第二个是一个真正的问题,因为我需要知道在运行循环之前查询了多少个帖子,因为我不想构建空的子菜单或其中只有一两个项目。
另一种方法是运行我自己的查询,但我不太清楚运行以查找所选类别中的帖子的正确语句。我可以想象搜索两个类别需要发生什么,但在 3+ 时它会变得模糊。
我可以执行多个独立的查询,然后使用 array_intersect 来查找与所有查询匹配的查询,但我想在这里扩展一下我的 mysql 和/或 wp 知识。
总之,我希望回答以下任何一个问题:
1 - query_posts() 的替代方案,允许多个 AND 匹配
2 - 一种查找使用 query_posts() 时将循环的帖子数量的方法
3 - 一些 mysql 魔法可以对同一个表的多个查询运行一个大联合
提前致谢
I am working on a wordpress theme and need to select posts that are in multiple categories to build a menu. I could set it up to work off of the tags, or a combination of tags and categories, but in the end I need to be able to search by multiple cat/tag. The only built-in wordpress function that I have been able to find that allows for a multiple AND search on cat/tag is the query_posts function as such:
query_posts(array('category__and' => array(list_of_cats)))
There are two problems with that. The first is that the wp docs say not to use it. The second is a real problem in that I need to know how many posts were queried before running my loop since I don't want to build submenus that are empty or only have one or two items in them.
The alternative is to run my own query but I can't quite figure out the right statement to run to find posts in the selected categories. I can visualize what needs to happen to search for two categories, but it gets fuzzy at 3+.
I can execute multiple, independent queries and then use array_intersect to find the those that match all queries but I would like to expand my mysql and/or wp knowledge a bit here.
In summary, I would appreciate an answer to any one of the following questions:
1 - an alternative to query_posts() that allows for multiple AND matching
2 - a way to find the number of posts that will be looped when using query_posts()
3 - some mysql magic that runs a big union on multiple queries to the same table
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“如果您想在主循环之外创建单独的循环,则应该使用 get_posts() 注意:从
版本 3.0 开始,页面 ID 数组也可用于包含和排除参数。”
很确定您可以执行以下操作:
获取查询帖子的总数。
是的,根据 WPQuery:
:)
"If you want to create separate Loops outside of the main one, you should use get_posts() instead.
Note: Beginning with Version 3.0, an array of Page ID also can be used for the include and exclude parameters."
Pretty sure you can do something like:
To get the total number of queried posts.
Yeah, according to WPQuery:
:)