Created
April 2, 2024 08:30
-
-
Save joychetry/41b16c0c08d6d1506acf118b5c27afb2 to your computer and use it in GitHub Desktop.
Bricks Builder: Custom Query Option
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 | |
/* Add new query type controls to query options */ | |
add_filter( 'bricks/setup/control_options', 'bl_setup_query_controls'); | |
function bl_setup_query_controls( $control_options ) { | |
/* Adding new options in the dropdown */ | |
$control_options['queryTypes']['goodmonks_crp_query'] = esc_html__( 'GoodMonks: CRP' ); | |
return $control_options; | |
}; | |
/* Run new query if option selected */ | |
add_filter( 'bricks/query/run', 'bl_maybe_run_new_queries', 10, 2); | |
function bl_maybe_run_new_queries( $results, $query_obj ) { | |
if ( $query_obj->object_type === 'goodmonks_crp_query' ) { | |
$results = run_first_query(); | |
} | |
return $results; | |
}; | |
/* Setup post data for posts */ | |
add_filter( 'bricks/query/loop_object', 'bl_setup_post_data', 10, 3); | |
function bl_setup_post_data( $loop_object, $loop_key, $query_obj ) { | |
/* setup post data if using any of our custom queries */ | |
if ( $query_obj->object_type === 'goodmonks_crp_query' ) { | |
global $post; | |
$post = get_post( $loop_object ); | |
setup_postdata( $post ); | |
} | |
return $loop_object; | |
}; | |
/* first WP Query arguments */ | |
function run_first_query() { | |
/* Add all of your WP_Query arguments here */ | |
$args = [ | |
'post_type' => 'post', | |
'posts_per_page' => '6', | |
]; | |
$posts_query = new CRP_Query( $args ); | |
return $posts_query->posts; | |
}; | |
/* Add new query type controls to query options */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment