{"id":233,"date":"2020-04-22T06:24:32","date_gmt":"2020-04-22T06:24:32","guid":{"rendered":"https:\/\/localhost\/bricksacademy\/?post_type=bricks_article&p=233"},"modified":"2021-07-06T09:47:21","modified_gmt":"2021-07-06T09:47:21","slug":"query-control","status":"publish","type":"bricks_article","link":"https:\/\/academy.bricksbuilder.io\/article\/query-control\/","title":{"rendered":"Query Control"},"content":{"rendered":"\n

The query control lets you set query arguments to retrieve items of any post type. Use the returned value to set up a custom WP_Query<\/code> to render the matching posts in any way you want.<\/p>\n\n\n\n

class Prefix_Element_Posts extends \\Bricks\\Element {\n  \/\/ Set builder controls\n  public function set_controls() {\n    $this->controls['exampleQueryArgs'] = [\n      'tab' => 'content',\n      'label' => esc_html__( 'Posts', 'bricks' ),\n      'type' => 'query',\n      \/\/ Default required for query to populate\n      'default' => [\n        'post_type' => 'post',\n      ],\n    ];\n  }\n\n  \/\/ Render element HTML\n  public function render() {\n    $query_args = $this->settings['exampleQueryArgs'];\n    $posts_query = new WP_Query( $query_args );\n\n    \/\/ Standard WordPress loop\n    if ( $posts_query->have_posts() ) :\n      while ( $posts_query->have_posts() ) : $posts_query->the_post();\n        \/\/ Render post title and thumbnail\n        the_title( '<h5>', '<\/h5>' );\n        the_post_thumbnail( 'thumbnail' );\n      endwhile;\n\n      wp_reset_postdata();\n    else :\n     esc_html_e( 'No posts matched your criteria.', 'bricks' );\n    endif;\n  }\n}<\/code><\/pre>\n\n\n\n

Resources<\/h3>\n\n\n\n