Custom Contact Search Filters

📅 Published on

📝 Last updated

We’re currently revamping our documentation for 4.0.
See old documentation →

Documentation

»

Developer Guides

»

Custom Contact Search Filters

Want to add your own custom search filters to Groundhogg? It’s easy! You just need two files.

You need a PHP file to register the filters in PHP to modify the search query, and a JavaScript file that will register the UI components on the frontend.

I recommend exploring the following files in Groundhogg to gain a deeper understanding of search filters.

  • groundhogg/includes/contact-query.php
  • groundhogg/db/query/table-query.php
  • groundhogg/db/query/query.php
  • groundhogg/db/query/where.php
  • groundhogg/db/query/join.php
  • groundhogg/db/query/filters.php

The PHP

You can add this to an existing plugin, your theme, or as a code snippet. You need to make modifications in the code to add the search logic you want, as well as change the file paths to reflect your inclusion method.

<?php

namespace MyCustomPlugin;

use Groundhogg\Contact_Query;
use Groundhogg\DB\Query\Filters;
use Groundhogg\DB\Query\Query;
use Groundhogg\DB\Query\Where;

class MyCustomFilters {

	public function __construct() {

		// when the filters need to be registered
		add_action( 'groundhogg/contact_query/filters/register', [ $this, 'register_filters' ] );
		add_action( 'groundhogg_enqueue_filter_assets', [ $this, 'enqueue_assets' ] );
	}

	/**
	 * Enqueue your custom filter js file
	 *
	 * @return void
	 */
	public function enqueue_assets() {
		wp_enqueue_script( 'my-custom-search-filters', plugins_url( '/path/to/filters.js' ) , [
			'groundhogg-admin-filter-contacts' // ensure the contact filters are declared as a dependency
		] );
	}

	/**
	 * Register any custom filters
	 *
	 * @param Filters $filters
	 *
	 * @return void
	 */
	public function register_filters( Filters $filters ) {
		$filters->register( 'left_join_example', [ $this, 'left_join_example' ] );
		$filters->register( 'sub_query_example', [ $this, 'sub_query_example' ] );
	}

	/**
	 * An example that uses left join
	 *
	 * @param array         $filter
	 * @param Where         $where
	 * @param Contact_Query $query
	 *
	 * @return void
	 */
	public function left_join_example( array $filter, Where $where, Contact_Query $query ) {

		// ->db = global $wpdb
		$table_name = $query->db->posts;

		$join = $query->addJoin( 'LEFT', $table_name );
		$join->onColumn( 'author_id', 'user_id' )
		     ->equals( 'post_status', 'publish' )
		     ->equals( 'post_type', 'custom_post_type' );

		$query->setGroupby( 'ID' );

		Filters::mysqlDateTime( "$join->alias.post_date", $filter, $where );
	}

	/**
	 * An example that uses a Sub Query
	 *
	 * @param array         $filter
	 * @param Where         $where
	 * @param Contact_Query $query
	 *
	 * @return void
	 */
	public function sub_query_example( array $filter, Where $where, Contact_Query $query ) {

		// ->db = global $wpdb
		$table_name = $query->db->posts;

		$postQuery = new Query( $table_name, 'posts' );
		$postQuery->setSelect( 'author_id' )
		          ->where()
		          ->equals( 'post_status', 'publish' )
		          ->equals( 'post_type', 'custom_post_type' );

		Filters::mysqlDateTime( 'post_date', $filter, $postQuery->where() );

		$where->in( 'ID', $postQuery );
	}

}

The JavaScript

Because both of our example filters are date based, we’ll use the createPastDateFilter factory function to create a filter with our date range UI.

(()=>{

  const {
    Filters,
    FilterRegistry,
    createFilter,
    createGroup,
    createDateFilter,
    createPastDateFilter,
    createStringFilter,
    createNumberFilter,
    createTimeFilter,
    ContactFilterRegistry
  } = Groundhogg.filters
  
  // add a group for our custom filters
  ContactFilterRegistry.registerGroup( createGroup( 'examples', 'Examples' ) )
  
  // add our filters using the createPastDateFilter factory
  ContactFilterRegistry.registerFilter( createPastDateFilter( 'left_join_example', 'Left Join Example', 'examples' ) )
  ContactFilterRegistry.registerFilter( createPastDateFilter( 'sub_query_example', 'Sub Query Example', 'examples' ) )

})()

Was this helpful?

Let us know if this document answered your question. That’s the only way we can improve.