Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

This plugin allows you to easily add filters to any query loop block.

Provides 2 new blocks that can be added within a query loop block to allow filtering by either post type or a taxonomy. Also supports using the core search block to allow you to search.
Provides 2 new blocks that can be added within a query loop block to allow filtering by either post type or a taxonomy.

Additions to the search block:
- It supports using the core search block to allow you to search.
- It includes search in the custom fields of the post.

Compatible with both the core query loop block and the [Advanced query loop plugin](https://wordpress.org/plugins/advanced-query-loop/) (In fact, in order to use post type filters, use of the Advanced Query Loop plugin is required).

Expand Down
2 changes: 1 addition & 1 deletion build/post-type/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/taxonomy/index.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions build/taxonomy/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

$taxonomy = get_taxonomy( $attributes['taxonomy'] );

if ( $block->context['query']['inherit'] ) {
$query_var = sprintf( 'query-%s', $attributes['taxonomy'] );
$page_var = 'page';
$base_url = str_replace( '/page/' . get_query_var( 'paged' ), '', remove_query_arg( [ $query_var, $page_var ] ) );
} else {
if ( empty( $block->context['query']['inherit'] ) ) {
$query_id = $block->context['queryId'] ?? 0;
$query_var = sprintf( 'query-%d-%s', $query_id, $attributes['taxonomy'] );
$page_var = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
$base_url = remove_query_arg( [ $query_var, $page_var ] );
} else {
$query_var = sprintf( 'query-%s', $attributes['taxonomy'] );
$page_var = 'page';
$base_url = str_replace( '/page/' . get_query_var( 'paged' ), '', remove_query_arg( [ $query_var, $page_var ] ) );
}

$terms = get_terms( [
Expand Down
2 changes: 1 addition & 1 deletion build/taxonomy/view.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions inc/custom-fields-search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Custom fields search functionality.
*
* @package query-filter
*/

namespace HM\Query_Loop_Filter;

use WP_Query;

/**
* Check if the current query is a search query.
*
* @param WP_Query|null $query The query object.
* @return bool Whether this is a search query.
*/
function is_custom_search( $query = null ) {
if ( ! $query ) {
global $wp_query;
$query = $wp_query;
}

// Check if it's a standard search
if ( is_search() ) {
return true;
}

// Check if it's a query loop with search parameter
if ( $query && ! empty( $query->query_vars['s'] ) ) {
return true;
}

return false;
}

/**
* Join posts and postmeta tables for custom field search.
*
* @param string $join The JOIN clause of the query.
* @param WP_Query $query The WP_Query instance.
* @return string The modified JOIN clause.
*/
function cf_search_join( $join, $query = null ) {
global $wpdb;

if ( is_custom_search( $query ) ) {
$join .= ' LEFT JOIN ' . $wpdb->postmeta . ' ON ' . $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
}

return $join;
}

/**
* Modify the search query with posts_where to include custom fields.
*
* @param string $where The WHERE clause of the query.
* @param WP_Query $query The WP_Query instance.
* @return string The modified WHERE clause.
*/
function cf_search_where( $where, $query = null ) {
global $wpdb;

if ( is_custom_search( $query ) ) {
$where = preg_replace(
"/\(\s*" . $wpdb->posts . ".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(" . $wpdb->posts . ".post_title LIKE $1) OR (" . $wpdb->postmeta . ".meta_value LIKE $1)",
$where
);
}

return $where;
}

/**
* Prevent duplicates in search results.
*
* @param string $where The DISTINCT clause of the query.
* @param WP_Query $query The WP_Query instance.
* @return string The modified DISTINCT clause.
*/
function cf_search_distinct( $where, $query = null ) {
if ( is_custom_search( $query ) ) {
return "DISTINCT";
}

return $where;
}
5 changes: 5 additions & 0 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ function bootstrap() : void {

// Query.
add_filter( 'render_block_core/query', __NAMESPACE__ . '\\render_block_query', 10, 3 );

// Custom field search.
add_filter( 'posts_join', __NAMESPACE__ . '\\cf_search_join', 10, 2 );
add_filter( 'posts_where', __NAMESPACE__ . '\\cf_search_where', 10, 2 );
add_filter( 'posts_distinct', __NAMESPACE__ . '\\cf_search_distinct', 10, 2 );
}

/**
Expand Down
3 changes: 2 additions & 1 deletion query-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Description: Filter blocks for the query loop utilising the interactivity API.
* Requires at least: 6.6
* Requires PHP: 8.0
* Version: 0.2.1
* Version: 0.2.2
* Author: Human Made Limited
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand All @@ -18,5 +18,6 @@
const ROOT_DIR = __DIR__;

require_once __DIR__ . '/inc/namespace.php';
require_once __DIR__ . '/inc/custom-fields-search.php';

bootstrap();