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.

custom post type excerpt on tag archive

  • I’m trying to insert a dynamic block on tag archives from a custom post type (“post_type_name”) with the exact same title as the tag.
    Therefor I added a block element as Page Hero with a query loop inside, looping over all elements of the custom post type, restricting posts per page to 1.
    The grid got a custom css class (“custom-css-class”), then I added the php-snippet:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'custom-css-class' ) !== false
        ) {
    		return array_merge( $query_args, array(
    			'meta_query' => array( 
    				array(
    				'post_type' => 'post_type_name',
                    		'name' => get_queried_object()->name
                    )
                ),
            ));
        }
    	
        return $query_args;
    
    }, 10, 2 );

    The results are empty unfortunately. Any suggestions on how to solve this?

  • Hi there,

    The post you want to insert to the tag archive is a custom post type? what is the relationship between this post and the tag name?

  • Thanks for your fast reply.

    Their title is identical. For example:
    The Custom-Post-Type: City. There is a post from this CPT called “Chicago”.
    Then there are regular posts with the regular tag “Chicago”.
    The tag archive for the tag “Chicago” should display the block element with the excerpt from the Custom-Post-Type “City” titled “Chicago” and below the regular tag archive for the tag “Chicago”. Atleast that’s the plan. If there is an easier way of accomplishing this I’m thankful for suggestions.

  • I see, try this instead:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        if ( is_tag() &&
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'custom-css-class' ) !== false
        ) {
            $tag = get_queried_object();
            
            return array_merge( $query_args, array(
                'post_type' => 'city', // Replace with your custom post type
                'name'      => $tag->name,
            ));
        }
    
        return $query_args;
    }, 10, 2 );
  • That works, thank you. I wasn’t so far off I guess.

  • You are welcome!
    Your code was pretty close 🙂

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