{"id":10302,"date":"2023-05-21T20:31:56","date_gmt":"2023-05-22T03:31:56","guid":{"rendered":"https:\/\/codedcommerce.com\/?page_id=10302"},"modified":"2025-01-14T11:15:11","modified_gmt":"2025-01-14T19:15:11","slug":"query-loop-customization","status":"publish","type":"woo-code","link":"https:\/\/codedcommerce.com\/woo\/query-loop-customization\/","title":{"rendered":"Query Loop customization"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Show only Draft posts (temporary use):<\/p>\n\n\n\n<pre class=\"wp-block-code language-php\"><code>add_filter( 'pre_render_block', function( $pre_render ) {\n\n\tadd_filter( 'query_loop_block_query_vars',\n\t\tfunction( $query ) {\n\t\t$query&#91;'post_status'] = 'draft';\n\t\treturn $query;\n\t} );\n\n\treturn $pre_render;\n\n} );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using an ACF field in this example to customize the WordPress Query Loop Block pre-rendered posts:<\/p>\n\n\n\n<pre class=\"wp-block-code language-php\"><code>add_filter( 'pre_render_block', function( $pre_render, $parsed_block ) {\n\n\t\/\/ Require ACF Plugin\n\tif( ! function_exists( 'get_field' ) ) {\n\t\treturn $pre_render;\n\t}\n\n\t$term = get_queried_object();\n\n\t\/\/ Using Custom Field\n\t$custom_field = get_field( 'custom_field', $term );\n\tif( $custom_field ) {\n\t\tadd_filter( 'query_loop_block_query_vars',\n\t\t\tfunction( $query, $block ) use( $custom_field ) {\n\t\t\t$query&#91;'post__in'] = $custom_field;\n\t\t\treturn $query;\n\t\t}, 10, 2 );\n\t}\n\n\t\/\/ Using Current Post Category\n\tadd_filter( 'query_loop_block_query_vars',\n\t\tfunction( $query, $block ) use( $term ) {\n\t\t$query&#91;'post__not_in'] = &#91; get_the_ID( $term ) ];\n\t\tforeach( get_the_category( $term ) as $category_term ) {\n\t\t\t$query&#91;'category_name'] = $category_term-&gt;slug;\n\t\t}\n\t\treturn $query;\n\t}, 10, 2 );\n\n\t\/\/ Return\n\treturn $pre_render;\n\n}, 10, 2 );<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-10302","woo-code","type-woo-code","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/woo-code\/10302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/woo-code"}],"about":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/types\/woo-code"}],"wp:attachment":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/media?parent=10302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}