I have this query to show sold out products:
<?php
//* Add new query type control to query options */
add_filter('bricks/setup/control_options', 'bl_setup_product_query_controls');
function bl_setup_product_query_controls($control_options) {
/* Adding a new option in the dropdown */
$control_options['queryTypes']['out_of_stock_products'] = esc_html__('Out of Stock Products');
return $control_options;
}
/* Run new query if option selected */
add_filter('bricks/query/run', 'bl_maybe_run_product_query', 10, 2);
function bl_maybe_run_product_query($results, $query_obj) {
if ($query_obj->object_type !== 'out_of_stock_products') {
return $results;
}
/* If option is selected, run our new query */
if ($query_obj->object_type === 'out_of_stock_products') {
$results = run_product_query();
}
return $results;
}
/* Setup post data for posts */
add_filter('bricks/query/loop_object', 'bl_setup_product_post_data', 10, 3);
function bl_setup_product_post_data($loop_object, $loop_key, $query_obj) {
if ($query_obj->object_type !== 'out_of_stock_products') {
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_product_query() {
$category_order = array(131, 129, 130);
$args = [
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => [
[
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category_order,
'include_children' => false
],
],
'meta_query' => [
[
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '='
],
],
];
$query = new WP_Query($args);
$products = [];
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$products[] = get_the_ID();
}
}
wp_reset_postdata();
// Sort the products by the specified category order
usort($products, function($a, $b) use ($category_order) {
$a_term = wp_get_post_terms($a, 'product_cat', ['fields' => 'ids']);
$b_term = wp_get_post_terms($b, 'product_cat', ['fields' => 'ids']);
return array_search($a_term[0], $category_order) - array_search($b_term[0], $category_order);
});
return $products;
}
It does work, post(product) titles are showing, post_url works, but featured image is empty…