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 610 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:
Filtering Breadcrumbs in Bricks

Filtering Breadcrumbs in Bricks

Using the bricks/breadcrumbs/items filter to modify Bricks' breadcrumbs’ element programmatically.
Categories:
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:
Pro
Programmatically populating ACF field values in WordPress

Programmatically populating ACF field values in WordPress

An example of how to set the values of a Select-type field with the names and labels of all public post types.
Categories: