Show pages and articles in category search result

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:

Read More

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

Related posts

Leave a Reply

2 comments

  1. To display pages in a category archive index:

    1. Add category taxonomy to the Page post-type

      Static pages, by default, do not have any taxonomies associated with them, including the category taxonomy. So you need to register the category taxonomy for the page post type, using register_taxonomy_for_object_type():

      function wpse94150_register_category_taxonomy_for_page_post_type() {
          register_taxonomy_for_object_type( 'category', 'page' );
      }
      add_action( 'admin_init', 'wpse94150_register_category_taxonomy_for_page_post_type' );
      
    2. 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.

    3. 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 via pre_get_posts:

      function wpse94150_filter_pre_get_posts( $query ) {
          // Only modify the main loop query
          // on category archive index pages
          if ( $query->is_main_query && $query->is_category() ) {
              // Return both posts and pages
              $query->set( 'post_type', array( 'post', 'page' ) );
          }
      }
      add_action( 'pre_get_posts', 'wpse94150_filter_pre_get_posts' );
      
    4. 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

    1. Where do I enter the code from step 3?

    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.

    And 2. Will this change be stable if I update WordPress or it’s plugins?

    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.)

  2. 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.