• jordydme

    (@jordydme)


    I created a FAQ custom post type; however, when you click the FAQ category link at the base of an FAQ post, the archive shows no results.
    Here is the code used for the CPT. Thanks for taking a look.

    function custom_post_type() {
      
    // Set UI labels for Custom Post Type
        $labels = array(
            'name'                => _x( 'FAQs', 'Post Type General Name', 'twentythirteen' ),
            'singular_name'       => _x( 'FAQ', 'Post Type Singular Name', 'twentythirteen' ),
            'menu_name'           => __( 'FAQs', 'twentythirteen' ),
            'parent_item_colon'   => __( 'Parent FAQ', 'twentythirteen' ),
            'all_items'           => __( 'All FAQs', 'twentythirteen' ),
            'view_item'           => __( 'View FAQ', 'twentythirteen' ),
            'add_new_item'        => __( 'Add New FAQ', 'twentythirteen' ),
            'add_new'             => __( 'Add New', 'twentythirteen' ),
            'edit_item'           => __( 'Edit FAQ', 'twentythirteen' ),
            'update_item'         => __( 'Update FAQ', 'twentythirteen' ),
            'search_items'        => __( 'Search FAQ', 'twentythirteen' ),
            'not_found'           => __( 'Not Found', 'twentythirteen' ),
            'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
        );
          
    // Set other options for Custom Post Type
          
        $args = array(
            'label'               => __( 'faqs', 'twentythirteen' ),
            'description'         => __( 'Frequently asked questions', 'twentythirteen' ),
            'labels'              => $labels,
            'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 5,
            'can_export'          => true,
            'has_archive'         => true,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'capability_type'     => 'page',
            'show_in_rest'        => true,
              
            // This is where we add taxonomies to our CPT
            'taxonomies'          => array( 'category' ),
        );
          
        // Registering your Custom Post Type
        register_post_type( 'faqs', $args );
      
    }
      
    /* Hook into the 'init' action so that the function
    * Containing our post type registration is not 
    * unnecessarily executed. 
    */
      
    add_action( 'init', 'custom_post_type', 0 );

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Category term queries by default only look for post post types with the requested term. If you want such queries to include your CPT you’ll need to add it to the “post_type” query arg’s array via the “pre_get_posts” action hook.
    The linked ref page contains a number of example code snippets, though sadly I don’t think any are specific to your need. However you should be able to cobble something together that’ll work from the examples and with some thoughtful modifications.

    Thread Starter jordydme

    (@jordydme)

    Thanks for your reply. Unfortunately this is a bit over my head. Had I used a plugin to create the CPT, would this functionality been baked in? Wondering if there is a way to do this without requiring custom code?

    Moderator bcworkz

    (@bcworkz)

    A CPT plugin may or may not be modifying category queries to include CPTs. It depends upon what UX the plugin author was going for. You could try out a few CPT plugins and see for yourself.

    If you were to also create a custom taxonomy associated with your CPT, then requesting a term’s archive would certainly involve the CPT. You’re encountering difficulty because you’ve associated a default taxonomy with your CPT. If you use custom code to register your CPT, you need additional custom code to modify category archive queries. The only way around this is as I’ve mentioned above.

    I realize that when something is over one’s head, there’s no way to judge if it’s only a little over or way over. If you managed to register a CPT you could likely manage the query modification as well. The following ought to work for you. It’s not too much of a departure from one of the examples on the ref page I linked to earlier. I just added a check for a not empty category name query var.

    function add_custom_pt( $query ) {
      if ( !is_admin() && $query->is_main_query() ) {
        if ( ! empty( $query->get('category_name'))) {
          $query->set( 'post_type', array( 'post', 'faqs' ) );
        }
      }
    }
    add_action( 'pre_get_posts', 'add_custom_pt' );

    This will not be valid for any kind of category request, but should work for a request such as example.com/category/foo/.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Post Type Categories Not Displaying On Archive Page’ is closed to new replies.