I’m using “Map Categories to Pages” to add categories to pages.
When I call the link http://mypage.com/category/catA+catB I get all my articles, that are associated to the categories catA and catB. What I’m looking for is:
A) a way to do something similar with pages: call http://mypage.com/pages/catA+catB and get all pages that are associates to the categories catA and catB
-or-
B) a way to get first pages and then articles, on the category search result page, like:
- Some Page
- Another Page
- Some Article
- Another Article
- One more Article
To display pages in a category archive index:
Add category taxonomy to the
Page
post-typeStatic pages, by default, do not have any taxonomies associated with them, including the
category
taxonomy. So you need to register thecategory
taxonomy for thepage
post type, usingregister_taxonomy_for_object_type()
:Create Pages with Categories
Self-explanatory. For pages to appear in a category archive index, you’ll need to have pages that have categories assigned.
Filter Query to include the page post-type for Category archive index pages
Next, you need to tell WordPress to include the
'page'
post-type in the results for the category archive index query, by filtering the$query
object viapre_get_posts
:View category archive index pages, now with posts and pages
Navigate to
example.com/category/cat-a
, and if you have Pages assigned to “Cat A”, you will see them in the archive index.Edit
I would put the code from step 3 in the same place you put the code from step 1: ideally in a site functionality Plugin, or as a less-ideal backup, in the
functions.php
file of a Child Theme.Since these changes are not made directly to any core or Plugin file: yes, the changes are stable and future-proof. (Unless WordPress changes one of the underlying APIs or hooks, which is extremely doubtful.)
Turned out, the “Map Categories to Pages” plugin has ist own Settings pages with an option “Show the Pages on Category pages“, which does exactly what I want.
I marked Chip Bennett’s answer anyway because it solves the problem without the plugin and it’s very detailed. Thank’s Chip.