Querying for Posts Starting with a Specific Letter in Bricks

Looking to show all posts that begin with a letter, like say A in Bricks?

Set Posts per page to a number that is larger than the total number of posts. Ex.: If there are 100 published posts, set Posts per page to say 200 (a realistic number of max no. of posts that will ever be published).

Then add this in child theme‘s functions.php or a code snippets plugin:

function wpse_298888_posts_where( $where, $query ) {
	global $wpdb;

	$starts_with = esc_sql( $query->get( 'starts_with' ) );

	if ( $starts_with ) {
		$where .= " AND $wpdb->posts.post_title LIKE '$starts_with%'";
	}

	return $where;
}
add_filter( 'posts_where', 'wpse_298888_posts_where', 10, 2 );

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
	if ( $element_id === 'lgbeyi' ) {
		$query_vars['starts_with'] = 'A';
	}
	
	return $query_vars;
}, 10, 3 );

Replace lgbeyi with the Bricks ID of your Query Loop enabled or Posts element.

Reference

https://wordpress.stackexchange.com/a/298913/14380

Get access to all 524 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Modifying ACF Field Value Before It Is Output

Modifying ACF Field Value Before It Is Output

Consider the scenario where a Page has a "Page Fields" field group created with ACF Pro like this: Field name: scientific_coordinators (type Repeater)|__ Sub field…
Categories:
Pro
Filtering a Bricks Terms query on Term archive pages

Filtering a Bricks Terms query on Term archive pages

On product category archives, how to show product type names but only those that the current product category has.
Bricks single mode – Limiting the number of posts to 1 in the editor

Bricks single mode – Limiting the number of posts to 1 in the editor

Creating a "Single Mode" for Bricks query loops to make sure only one post is rendered in the builder.