Hello,
Because the default bricks item ( upsell/crosssell) element is not really customisable
I am creating a new query loop type using Bricks Lab code : Adding any Custom WP_Query loop to Bricks' Query Loop - BricksLabs
I manage to display all the products but…
After trying for a couple hours i still cant set the arguments of the query to display cross-sell products.
Idk, I feel so stupid…Please help.
Thank you !
/* Add new query type control to query options */
add_filter( ‘bricks/setup/control_options’, ‘bl_setup_query_controls’);
function bl_setup_query_controls( $control_options ) {
/* Adding a new option in the dropdown */
$control_options['queryTypes']['cross-sell'] = esc_html__( 'Cross Sell' );
return $control_options;
};
/* Run new query if option selected */
add_filter( ‘bricks/query/run’, ‘bl_maybe_run_new_query’, 10, 2);
function bl_maybe_run_new_query( $results, $query_obj ) {
if ( $query_obj->object_type !== 'cross-sell' ) {
return $results;
}
/* If option is selected, run our new query */
if ( $query_obj->object_type === 'cross-sell' ) {
$results = run_new_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 ) {
if ( $query_obj->object_type !== 'cross-sell' ) {
return $loop_object;
}
global $post;
$post = get_post( $loop_object );
setup_postdata( $post );
return $loop_object;
};
/* Return results from our custom WP Query arguments */
function run_new_query() {
/* Add all of your WP_Query arguments here */
$args = [
'post_type' => 'product',
'orderby' => 'rand',
'posts_per_page' => '99',
];
$posts_query = new WP_Query( $args );
return $posts_query->posts;
};