David Browne wrote Building Prev / Next Posts with Query Loops in Bricks tutorial in the past in which he showed how to add a custom “Adjacent Posts” option in the Query type dropdown for Bricks‘ query loops. The previous and next posts were based on the post’s published date.

This Pro tutorial is a variation of the above for single posts where the previous and next posts are based on the value of a number-type custom field.

We are going to add a “Post Number” custom field using ACF (any other plugin like Meta Box /method can also be used), then assign numbers like 1, 2, 3, 4 (need not be sequential although it helps if they are). The objective is to show Post 2 as the previous post and Post 4 as the next post when viewing Post 3 for example.

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

Step 1

/**
 * Query all the posts and order the ones that have post_number meta set by post_number meta value
 */
function bl_get_post_ids_ordered_by_meta() {
	// Only return IDs
	$args = array(
		'post_type' => 'post',
		'posts_per_page' => 100, // set this to a number higher than the number of posts you have
		'orderby' => 'meta_value_num',
		'meta_key' => 'post_number',
		'order' => 'ASC',
		'fields' => 'ids'
	);

	$query = new WP_Query( $args );
	
	return $query->posts;
}

/* Add new adjacent type control to query loop options */
add_filter( 'bricks/setup/control_options', function( $control_options ) {
	/* Adding a new option in the dropdown */
	$control_options['queryTypes']['adjacent_posts_orderedby_post_meta'] = esc_html__( 'Adjacent Posts Ordered by Post Meta' );

	return $control_options;
} );

/* Run new query if option selected */
add_filter( 'bricks/query/run', function( $results, $query_obj ) {
	if ( $query_obj->object_type !== 'adjacent_posts_orderedby_post_meta' ) {
		return $results;
	}
	
	/* If option is selected, run our new query */
	if ( $query_obj->object_type === 'adjacent_posts_orderedby_post_meta' ) {
		$results = bl_run_new_query_20230223();
	}
	
	return $results;
}, 10, 2 );

/* Setup post data for posts */
add_filter( 'bricks/query/loop_object', function( $loop_object, $loop_key, $query_obj ) {
	if ( $query_obj->object_type !== 'adjacent_posts_orderedby_post_meta' ) {
		return $loop_object;
	}

	global $post;
	$post = get_post( $loop_object );
	setup_postdata( $post );
	
	 return $loop_object;
}, 10, 3 );

/* Return results from our custom WP Query arguments */
function bl_run_new_query_20230223() {
	$posts = bl_get_post_ids_ordered_by_meta();

	// Get the position of the current post in the ordered array of all posts
	$pos = array_search( get_the_ID(), $posts );

	$prev_pos = $pos - 1;
	$next_pos = $pos + 1;

	$prev_post_id = isset( $posts[$prev_pos] ) ? $posts[$prev_pos] : '';
	$next_post_id = isset( $posts[$next_pos] ) ? $posts[$next_pos] : '';

	$args = [ 
		'post_type' => 'post',
		'posts_per_page' => '2',
		'post__in' => [ 
			$prev_post_id, 
			$next_post_id
		],
		'orderby' => 'post__in',
	];
	
	$posts_query = new WP_Query( $args );

	return $posts_query->posts;
};

Replace post_number with the name/ID of your number-type custom field.

Step 2

Edit your Bricks template that applies to single posts.

Add a Section and inside its Container, add a Container having a Block. Enable query loop on a Block.

Click the loop icon and select “Adjacent Posts Ordered by Post Meta” for Type.

Add any elements inside the Block like Post Title and a featured image as needed.

References

https://pluginrepublic.com/get-next-post-previous-post-meta-key/

https://www.php.net/manual/en/function.array-key-exists.php