Category For Pages In WordPress

I need to categorize search result of WordPress, I have done that through adding categories to posts and custom post types. Now, only pages are listing without any category, I think it is possible to add category to pages as well, but I am not aware of its consequences.

Kindly you share your thoughts and experience.

Read More

Thank you

Related posts

2 comments

  1. WordPress doesn’t provides option to create category in pages .

    One thing that can be done is modification in search query
    This will search you in all the pages only.

    function SearchFilter($query) {
        if ($query->is_search) {
           $query->set('post_type', 'page');
        }
        return $query;
        }
    
        add_filter('pre_get_posts','SearchFilter');
    

    Add this hook on some specific conditions, when you want only pages to be displayed in searches.

    Other side will be to display everything other than pages . In that you will have to exclude the pages by their ID’s

    function SearchFilter($query) {
        if ($query->is_search) {
           $excludeId = array(23,23,23);
           $query->set('post__not_in', array($excludeId));
        }
        return $query;
        }
    
        add_filter('pre_get_posts','SearchFilter');  
    
  2. WordPress does not natively have a global taxonomy which is what you want. Categories in WordPress are tied to blog posts.

    To accomplish this, I would use a combination of the incredible Advanced Custom Fields (ACF) plugin and modifying functions.php to add a custom taxonomy to your site and apply it to all of your post-types.

    Step 1

    In functions.php, make a custom taxonomy like this (modify for your needs):

    // add_action registers the taxonomy into wordpress
    add_action( 'init', 'setup_my_tax' );
    
    // this function sets up the taxonomy to whatever standards you want
    // reference register_taxonomy on codex.wordpress.org
    function setup_my_tax() 
    
      // first parameter becomes the slug of the tax
      register_taxonomy( 'capabilities', array( 'post' ), array(
    
        // labels will determine how it show up in a menu
        'labels' => array(
          'add_new_item' => 'Add New ',
          'all_items' => 'All Capabilities',
          'edit_item' => 'Edit Capability',
          'menu_name' => 'Capabilities',
          'name' => 'Capabilities',
          'new_item' => 'New Capability',
          'not_found' => 'No Capabilities Found',
          'not_found_in_trash' => 'No Capabilities Found in Trash',
          'parent' => 'Parent of Capability',
          'search_items' => 'Search Capabilities',
          'singular_name' => 'Capability',
          'view_item' => 'View Capability'
        ),
    
        // important individual settings from register_taxonomy
        'hierarchical' => true,
        'public' => true,
        'query_var' => true,
        'show_admin_column' => true,
        'show_ui' => true
      ));
    }
    

    Step 2

    With ACF installed, you’ll use the GUI to make a custom field set that contains this taxonomy. Below the custom field set are rule options that show you how to apply the custom bit to all of your entities.

    enter image description here

    Step 3

    In page.php and other templates you can make reference to your taxonomy terms like this:

    // Put the capabilities into an array variable
    $capObjects = get_field('capabilities');
    
    // Iterate through the the array of tags and output them
    // In WordPress, you have to use the term ID to lookup the term name
    foreach ($capObjects as $capObject):
      echo '<li class="capabilities-item">';
      echo '<a href="' . get_term_link($capObject) . '">' . $capObject->name . '</a> ';
      echo '</li>';
    endforeach;
    

    You can now adjust your search template by a true tag that spans all of your types of content.

Comments are closed.