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.

Dynamic value for “select terms” in query

  • Hi

    I have a custom post type of logos
    The logos are assigned a category

    I have created an element which is a query on the logos to select all logos with a specific category, by using TAXONOMIES and SELECT TERMS

    This works

    But I want to hook the element to each of my product pages and only show the logos relevant to that product page based on the category that is appropriate for that page.

    I use ACF and my thought was for each product page, to populate an ACF field that contained the category value and then somehow use that value in the query.

    Is there a way to alter the ‘SELECT TERMS’ value of the query in some dynamic way to match the category for the product page it is on.

    I’m happy to use PHP

    Thanks

  • Hi there,

    I use ACF and my thought was for each product page, to populate an ACF field that contained the category value and then somehow use that value in the query.

    You can use the approach above. Then try adding this PHP snippet:

    add_filter( 'generateblocks_query_wp_query_args', function( $query_args, $attributes, $block, $current ) {
    	$category = get_post_meta( $current['post_id'], 'your_custom_field_key', true );
    
    	if ( empty( $category ) ) {
    		return $query_args;
    	}
    
    	if ( ! isset( $query_args['tax_query'] ) ) {
    		$query_args['tax_query'] = [];
    	}
    
    	$query_args['tax_query'][] = [
    		'taxonomy' => 'your_cpt_taxonomy',
    		'field'    => 'slug',
    		'terms'    => $category,
    	];
    
    	return $query_args;
    }, 10, 4 );

    Replace:
    your_custom_field_key — the custom field meta key on the product page.
    your_cpt_taxonomy — the taxonomy slug of the Logos CPT’s categories.
    slug in 'field' — change to name or term_id depending on what the custom field stores.

    Make sure you don’t add any parameters to the Query block.

  • Thank you so much – this is brilliant!!!!!

    Just wondering whether I should be checking I’m on a product page first??
    I realize that $category won’t have a value if I’m not on a product page but wondering if it would be more efficient to check first?

    Also – a caveat for anyone else following along, I had more than one query on my page so I had to check I had the right query before updating the $query_args

    I checked this value: $attributes[‘query’][‘post_type’][0] to see if it matched my logos custom post type.

    Oh and by the way, my query was inside a carousel and all still worked OK!

  • I realize that $category won’t have a value if I’m not on a product page but wondering if it would be more efficient to check first?

    Alvind’s code does check if $category has values first by this piece:

    if ( empty( $category ) ) {
    		return $query_args;
    	}

    Do you want to check if it’s on a product page first?

  • Hi Ying

    Sorry my question was a bit ambiguous

    I said ‘would it be more efficient to check first’ – meaning check that it was a product page first

    So I’m asking if it would be more efficient to check if it is a product page first

    Thanks

  • Also – a caveat for anyone else following along, I had more than one query on my page so I had to check I had the right query before updating the $query_args

    In that case, you can add a custom class of logos-cpt-query to the Query block under Advanced > Additional CSS Classes, then use this updated snippet:

    add_filter( 'generateblocks_query_wp_query_args', function( $query_args, $attributes, $block, $current ) {
    	if ( empty( $attributes['className'] ) || strpos( $attributes['className'], 'logos-cpt-query' ) === false ) {
    		return $query_args;
    	}
    
    	$category = get_post_meta( $current['post_id'], 'your_custom_field_key', true );
    
    	if ( empty( $category ) ) {
    		return $query_args;
    	}
    
    	if ( ! isset( $query_args['tax_query'] ) ) {
    		$query_args['tax_query'] = [];
    	}
    
    	$query_args['tax_query'][] = [
    		'taxonomy' => 'your_cpt_taxonomy',
    		'field'    => 'slug',
    		'terms'    => $category,
    	];
    
    	return $query_args;
    }, 10, 4 );

    This way, the snippet will only apply to that specific Query block.

    So I’m asking if it would be more efficient to check if it is a product page first

    If the Element’s Display Rules location is already set to Product Pages, that additional condition wouldn’t be necessary, since the Element will only display on product pages anyway.

  • Thanks Alvind – perfect! Thank you so much for your excellent support.

  • You’re welcome! 🙂

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