I’m hoping to understand a bit more about the filter bricks/post/query_vars and maybe you smart people can enlighten me.
I have a single post template for the ‘blog’ section of a website. It includes a metabox.io field called selected_team_member which lets the user pick several team members to attach to a post. Nothing fancy. Easy to do in a code block.
But I wanted to see how to modify the Bricks query loop builder. But i may not understand how the filter works? Below is my code.
I am trying to modify the query only on the ID of the block wrapper not the main query.
According to my log this filter does get called and does produce results, just not specific to the IDs returned in the select_team_member array. It outputs any or all of them.
Why? Could the post object not be available?
Thanks in advance for helping me understand this better.
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
// Log message when this function is executed
error_log('The bricks/posts/query_vars filter was executed.');
if( $element_id !== 'brxe-qhqtbq') return $query_vars;
// Get the global post object
global $post;
// Get post IDs from the 'select_team_member' Metabox.io field
$post_ids = rwmb_meta('select_team_member', array(), $post->ID);
// Log the retrieved post_ids
error_log('post_ids retrieved: ' . implode(', ', $post_ids));
// If no posts are selected, set post__in to an empty array
if (empty($post_ids)) {
$query_vars['post__in'] = [];
} else {
$query_vars['post__in'] = $post_ids;
}
$query_vars['post_type'] = ['investment-team'];
$query_vars['orderby'] = 'post__in';
return $query_vars;
}, 10, 3 );
Hi @digismith, thanks that did provide some help! Was this documented anywhere? Found a few other issues, but I was able to use the modified code (see below) to allow the use of of the Query Loop UI in the builder, but modifying the some specific query vars via code.
A fews things stand out: I have to use a hacky solution to force the Query Loop from not displaying other content. For example. If the array select_team_member is empty is simply returned all entries regardless if they were attached to the post. If I forced the post_type only on the successful check (if the array has a value) it would return blog posts instead. Forcing it to a non-existant post type solved that issue, but seems a bit hacky to me and means I need to figure out a different solution.
The other issue is that if you a wrapper div of sorts to contain the cards, that will output regardless unless you create another function (this also holds true for bricks/query//related_posts// posts solutions), but is a very easy if statement in a codeblock. Also requires some thought in how these quer_vars and query_loop object are used.
Is there a better way of doing it? Im sure but I would hope someone (maybe from the bricks team) would shed some light on this!
<?php
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
// Log message when this function is executed
error_log('The bricks/posts/query_vars filter was executed.');
//if( $element_id !== 'brxe-qhqtbq') return $query_vars;
if( $element_id !== 'qhqtbq') return $query_vars;
// Get the global post object
global $post;
//error_log('The post Object: ' . var_dump($post));
error_log('The post ID: ' . ($post->ID));
// Get post IDs from the 'select_team_member' Metabox.io field
$post_ids = rwmb_meta('select_team_member', $post->ID);
// echo '<pre>';
// var_dump($post_ids);
// echo '</pre>';
// Log the retrieved post_ids
//error_log('post_ids retrieved: ' . $post_ids);
// If no posts are selected, set post__in to an empty array
if (empty($post_ids)) {
//error_log("No post Ids attached. Its empty. lets mess up the post_type so it returns nothing. Seems hacky.");
$query_vars['post__in'] = [];
$query_vars['post_type'] = ['null'];
} else {
//error_log("Not Empty. Lets set the post__in and post_type");
$query_vars['post__in'] = $post_ids;
$query_vars['post_type'] = ['investment-team'];
}
$query_vars['orderby'] = 'post__in';
return $query_vars;
}, 10, 3 );
Hi @digismith. Thanks for the link. I am using metabox.io for this site. The user can select another post type (team member), but either I am doing something wrong or the integration for metabox is missing. I’m hoping it is just me ;).
For the metabox.io i am using a field of type post, limited to the CPT Team Members. I am taking another look to see if there is a different approach to this, but if you or anyone else knows more please share.
Thanks. Just found that myself. I’ll have to re-eval my current setup and test it this way. I have had this working with O2 a long time ago, but why not in Bricks (or why I dint think of it) is a mystery…