-
kevinbk
How to display Loop query that contains the same custom_field value as the curreent post? I want to put together a dictionary and one of the things I want is to display words that have the same pronunciation on the page, so I already have custom_field with the romanized reading of the word, if it is equivalent the query_loop will display it.
-
Fernando
Hi Kevinbk,
To clarify, are you using a Query Loop Block?
If so, you can create a Shortcode to retrieve your custom field, and then use that shortcode in your Query Loop.
By default, WordPress uses the post meta of the actual Page/Post where the Loop is in for Shortcodes in Query Loops.
Reference: https://docs.generatepress.com/article/creating-a-shortcode/
-
kevinbk
But where does Query Loop Block accept shortcode? I even had to create a function in functions.php for it to accept a CSS to do the search with post title.
-
Fernando
You can add a Shortcode Block inside a Query Loop. Reference: https://wordpress.org/documentation/article/shortcode-block/
-
kevinbk
But in this case I want to use a meta_post to filter the search of the loop. Like, I want Loop to display only posts that have a certain meta_post “kana” equivalent to the meta_post “kana” of the current page.
-
David
Hi there,
you can use the
generateblocks_query_loop_args
filter to check set a Query Loop withyour-css-class
to show posts that have a matching$meta_key
that contains youryour_custom_field_key
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'your-css-class' ) !== false ) { $post_id = get_the_ID(); $meta_key = 'your_custom_field_key'; $current_post_custom_field = get_post_meta($post_id, $meta_key, true); return array_merge( $query_args, array( 'meta_query' => array( array( 'key' => $meta_key, 'value' => $current_post_custom_field, 'compare' => '=' ) ) ) ); } return $query_args; }, 10, 2 );
-
kevinbk
As I am going to use different custom_field on different pages, is there no code where I choose custom_field directly on the page in question instead of having to make several codes for different custom_field?
-
David
hmmm…. complicated.
If you had a custom field, which stores the name of the custom field that you set in the$meta_key
variable then i suppose you could do:add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'your-css-class' ) !== false ) { // set the name of the custom field that stores your custom field name $custom_field_choice = 'your_custom_field_choice_name'; $post_id = get_the_ID(); // load the $meta_key with the value of $custom_field_choice $meta_key = get_post_meta($post_id, $custom_field_choice, true); // get the current post meta of the chosen meta_key $current_post_custom_field = get_post_meta($post_id, $meta_key, true); return array_merge( $query_args, array( 'meta_query' => array( array( 'key' => $meta_key, 'value' => $current_post_custom_field, 'compare' => '=' ) ) ) ); } return $query_args; }, 10, 2 );
-
kevinbk
I’ll try, but I suggest adding a new custom_field function natively in generateblocks, it seems to be very useful.
-
David
Custom Meta parameters are something we look to add in the future.
But they can be super complicated due to various comparisons and nested arrays etc. and everybody seems to want something different 🙂For now it requires code. Tomorrow it may not.
-
kevinbk
What do you mean everyone wants something different? Maybe if the Query_loop search field accepts shortcodes already meet this need, so people can put different fields. Another option would also be to display in the query a custom_field Value equivalent to the current page.
One thing I noticed is that these codes that you add via CSS, it does not seem to be able to exclude the current_post from the results display, as you can see in this link: https://skdesu.com/significado/愛-ai/
Note that 愛 appears again in the two query_loop of similar words and words of the same pronunciation, thus generating a link to the same current page.
-
David
What do you mean everyone wants something different?
The use cases for a WP_query are many and varied. Possibility an uncountable amount of variations.
And we want to add a UI that covers as many cases as possible. Which we will address in a future update. But it will be a Pro feature, as these kinds of requests are custom development.Are you a GB Pro user? as that has the Exclude -> Current Post arg ?
- You must be logged in to reply to this topic.