Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Archive Page Query Loop for ACF (Categories not Working)

  • owlsliveintrees

    Hello,

    I’m trying to create an archive template for a CPT made with ACF using the Query block.

    It works for the NEW taxonomies I created with ACF, but it doesn’t work with the built-in category taxonomy.

    So website.com/format/in-person/ works fine, but website.com/category/hummus doesn’t bring up any of the posts.

    https://imgur.com/a/7pPgn5c

    Do the built-in category taxonomy just not work with CPTs?

  • Hi there,

    Did you assign the category taxonomy to the CPT?

    And as far as I know, category archive only displays post post type, no CPT.

    If you want to show CPT on category archive, add this PHP code. change cpt-a to the actual cpt slug.

    function include_cpt_in_category_archive( $query ) {
        // Check if we're on a category archive page and it's the main query, not in admin
        if ( ! is_admin() && $query->is_main_query() && $query->is_category() ) {
            // Include both 'post' and your custom post type
            $query->set( 'post_type', array( 'post', 'cpt-a' ) );
        }
    }
    add_action( 'pre_get_posts', 'include_cpt_in_category_archive' );

    Adding PHP: https://docs.generatepress.com/article/adding-php/

  • owlsliveintrees

    Hi Ying – creating a custom post type with ACF allows you to utilize the existing category taxonomy.

    It’s been working great until I tried to utilize a loop template to create an archive page element.

  • Hi there,

    In your Loop Template element, select the Query block and enable the “Inherit query from template” option.

    After that, add the snippet Ying provided above so that the custom post type entries are included in the category archive query.

  • owlsliveintrees

    WELL THAT WORKED.

    Thank you Ying. Alvin – you too.

  • No Problem 🙂

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.