Skip to content

Instantly share code, notes, and snippets.

@wplit
Last active May 23, 2023 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wplit/32030289385ecc23c87784cac0267a0e to your computer and use it in GitHub Desktop.
Save wplit/32030289385ecc23c87784cac0267a0e to your computer and use it in GitHub Desktop.
showing the difference between setting up a new query, or changing existing using the elements ID and the bricks/posts/query_vars filter
<?php
/*
* Snippet from the custom WP Query tutorial where the query arguments were added..
*/
function run_new_query() {
/* this is where you were adding your custom query arguments */
$args = [
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => '1',
];
$posts_query = new WP_Query( $args );
return $posts_query->posts;
};
/*
* Adding those same arguments using the filter is done like this..
*/
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
if ( $element_id === 'owxazg' ) { /* the owxazg needs to be the element ID */
/* this is the new place where you'd add your query arguments */
$query_vars['post_type'] = 'post';
$query_vars['orderby'] = 'rand';
$query_vars['posts_per_page'] = '1';
}
return $query_vars;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment