Support

Account

Home Forums General Issues Taxonomy Link to Custom Post Type Archive

Solved

Taxonomy Link to Custom Post Type Archive

  • Greetings,

    I have created an ACF Custom Field Group for a custom post type called “portfolio-cpt”. The Field Group contains the “Taxonomy” Field Type with “taxonomy” as Field Name. I then created 5 custom posts and assigned the category (Custom Taxonomy) of “Art” to each one.

    Using the following code in my ‘single-portfolio-cpt.php’ file I attempt to create a link to an archive of the 5 custom posts with the category of “Art”:

    <?php
    $term = get_field(‘taxonomy’);

    if( $term ): ?>

    $term->name: <?php echo $term->name; ?>

    $term->description: <?php echo $term->description; ?>

    $term->slug: <?php echo $term->slug; ?>

    $term->id: <?php echo $term->term_id; ?>

    get_term_link($term): <?php echo get_term_link( $term ); ?>

    “>View all ‘<?php echo $term->name; ?>’ posts

    <?php endif; ?>

    Output:

    $term->name: Art
    $term->description: Art
    $term->slug: art
    $term->id: 3
    get_term_link($term): http://www.themedev.dev/category/art/

    This link tries to pull up the regular posts (not the custom ones) with category ‘Art’ (and there are none). How can I display an archive of the 5 custom posts types with the category of ‘Art’?

    Thank you,
    Daniel

  • Hi @dniclas

    You say you’ve also registered a custom taxonomy connected with your portfolio-cpt but to me it seems you’ve rather created a term/category named art only. That means that when visiting the archive you’re visiting the archive of a regular category (wordpress builtin categories).

    If you want to only display your custom post type you’ll have to hook into the query for this archive and change the post_type OR actually register your own taxonomy and use that instead.

    Here’s a snippet you can add to your themes functions.php to change the archive of the specific “art” category to only display your CPT:

    
    function my_modify_queries( $query ) {
    	
    	//dont run on admin or not a main query
        if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if ( is_category('art') ) {
            // Display only 1 post for the original blog archive
            $query->set( 'post_type', 'portfolio-cpt' );
            return;
        }
    
    }
    add_action( 'pre_get_posts', 'my_modify_queries', 1 );
    
  • Hi Jonathan,

    You identified the problem exactly. I did not register the custom taxonomy in functions.php. If you had not told me how to do this and provided the snipped I would be lost, so thank you very much.

    I would like to point out that to make the archive work for the custom posts types (with the custom Taxonomy) I had to select the correct Taxonomy in the custom Taxonomy selection box and ALSO check off the identical Category Taxonomy in the standard WP Catagories box. I am totally ok with this, but want to point it out to check and make sure this is ok and won’t bite me down the road.

    Thank you for your help!
    Daniel

  • Hi Daniel,

    Glad I could help. By check off do you mean that you had to uncheck the “art” category in the builtin category-taxonomy for your custom taxonomys term “art” to work properly?

  • Hi Jonathan, Sorry for being unclear. I had to check the “art” checkbox in the Wp Catagories box (in addition to selecting the “art” taxonomy in the ACF Taxonomy select box).

    Daniel

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Taxonomy Link to Custom Post Type Archive’ is closed to new replies.