23rd Mar '23
/
0 comments

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 633 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

How to Add a Custom Control Field to an Element in Bricks

How to Add a Custom Control Field to an Element in Bricks

In this tutorial, we'll learn how to add a custom control field to an element and dynamically change a CSS property inside the builder. Introduction…
Categories:
Tags:
How to hide/remove the Block Element from the Bricks Builder

How to hide/remove the Block Element from the Bricks Builder

Don't like the new Block element in Bricks? Just copy/paste the following code into your child theme's functions.php. By the way, It's a terrible idea…
Categories:
Tags:
Filtering Breadcrumbs in Bricks

Filtering Breadcrumbs in Bricks

Using the bricks/breadcrumbs/items filter to modify Bricks' breadcrumbs’ element programmatically.
Categories:
How to Exclude Elements from Bricks Editor

How to Exclude Elements from Bricks Editor

Bricks has a handy bricks/builder/elements filter using which the available elements that show up after clicking + (or ⌃/⌘ + ⇧ + E) in the…
Categories:
Tags:
Pro
ACF Relationship Select Filter in Bricks

ACF Relationship Select Filter in Bricks

In the BricksLabs Facebook group, a user asks: I'm learning about ACF relationships and attempting to output a list of posts on any given page,…
Categories: