Last active
May 23, 2023 05:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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