PHPackages                             ed-itsolutions/cmb2-field-ajax-search - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ed-itsolutions/cmb2-field-ajax-search

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ed-itsolutions/cmb2-field-ajax-search
=====================================

CMB2

v2.0.0(5y ago)12.6k↓31.3%2GPL-2.0-or-laterPHPPHP ^5.3.3 || ^7.0

Since Jul 1Pushed 5y agoCompare

[ Source](https://github.com/Ed-ITSolutions/cmb2-field-ajax-search)[ Packagist](https://packagist.org/packages/ed-itsolutions/cmb2-field-ajax-search)[ RSS](/packages/ed-itsolutions-cmb2-field-ajax-search/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

CMB2 Field Type: Ajax Search
============================

[](#cmb2-field-type-ajax-search)

Custom fields for [CMB2](https://github.com/WebDevStudios/CMB2) to attach posts, users or terms to each others.

Formed from .

This fork removes the need to use this as a plugin and instead lets you require it using composer.

[![example](example.gif)](example.gif)

Once activated, this plugin adds three new field types `post_ajax_search`, `user_ajax_search` and `term_ajax_search`.

This plugin is an update of [CMB2 Field Type: Post Search Ajax](https://github.com/alexis-magina/cmb2-field-post-search-ajax) by [Magina](http://magina.fr/) with support to attach posts, users or terms.

Installation
------------

[](#installation)

Install from composer

```
composer require ed-itsolutions/cmb2-field-ajax-search

```

Make sure that your `functions.php` loads composer

```
require_once('vendor/autoload.php');
```

If the assets don't load you can use the filter `cmb2_field_ajax_search_url` to set it.

```
add_filter('cmb2_field_ajax_search_url', function(){
	return (get_template_directory_uri() . '/cmb2-ajax-search/');
});
```

Parameters
----------

[](#parameters)

Options :

- `multiple` (bool, default = false) : Turn field into a multiple attached objects
- `limit` (int, default = -1 : single selection) : Limit the number of posts that can be selected (-1 for unlimited)
- `sortable` (bool, default = false) : Allow selected items to be sort (only if multiple = true)
- `query_args` (array) : Query arguments to pass on each request

Query args:

- query\_args accepts same parameters as [WP\_Query](https://codex.wordpress.org/Class_Reference/WP_Query) for `post_ajax_search`
- query\_args accepts same parameters as [WP\_User\_Query](https://codex.wordpress.org/Class_Reference/WP_User_Query) for `user_ajax_search`
- query\_args accepts same parameters as [WP\_Term\_Query](https://developer.wordpress.org/reference/classes/wp_term_query/) for `term_ajax_search`

Examples
--------

[](#examples)

```
add_action('cmb2_admin_init', 'cmb2_ajax_search_metabox');

function cmb2_ajax_search_metabox(){
	$prefix = 'your_prefix_demo_';
	$cmb_demo = new_cmb2_box(array(
		'id'            => $prefix . 'metabox',
		'title'         => __( 'Attached posts Metabox', 'cmb2' ),
		'object_types'  => array( 'page', 'post' ), // Post type
	) );

	// Single post
	$cmb_demo->add_field( array(
		'name'          => __( 'Attached post', 'cmb2' ),
		'desc'          => __( 'Field description (optional)', 'cmb2' ),
		'id'            => $prefix . 'post',
		'type'          => 'post_ajax_search',
		'query_args'	=> array(
			'post_type'			=> array( 'post' ),
			'posts_per_page'	=> -1
		)
	) );

	// Multiple posts
	$cmb_demo->add_field( array(
		'name'          => __( 'Multiple posts', 'cmb2' ),
		'desc'          => __( 'Field description (optional)', 'cmb2' ),
		'id'            => $prefix . 'posts',
		'type'          => 'post_ajax_search',
		'multiple-items' => true,
		'limit'      	=> 10,
		'query_args'	=> array(
			'post_type'			=> array( 'post', 'page' ),
			'post_status'		=> array( 'publish', 'pending' )
		)
	) );

	// Single user
	$cmb_demo->add_field( array(
		'name'          => __( 'Attached user', 'cmb2' ),
		'desc'          => __( 'Field description (optional)', 'cmb2' ),
		'id'            => $prefix . 'user',
		'type'          => 'user_ajax_search',
		'query_args'	=> array(
			'role'				=> array( 'Subscriber' ),
			'search_columns' 	=> array( 'user_login', 'user_email' )
		)
	) );

	// Multiple users
	$cmb_demo->add_field( array(
		'name'          => __( 'Multiple users', 'cmb2' ),
		'desc'          => __( 'Field description (optional)', 'cmb2' ),
		'id'            => $prefix . 'users',
		'type'          => 'user_ajax_search',
		'multiple-items' => true,
		'limit'      	=> 5,
		'query_args'	=> array(
			'role__not_in'		=> array( 'Administrator', 'Editor' ),
		)
	) );

	// Single term
	$cmb_demo->add_field( array(
		'name'          => __( 'Attached term', 'cmb2' ),
		'desc'          => __( 'Field description (optional)', 'cmb2' ),
		'id'            => $prefix . 'term',
		'type'          => 'term_ajax_search',
		'query_args'	=> array(
			'taxonomy'			=> 'category',
			'childless'			=> true
		)
	) );

	// Multiple terms
	$cmb_demo->add_field( array(
		'name'          => __( 'Multiple terms', 'cmb2' ),
		'desc'          => __( 'Field description (optional)', 'cmb2' ),
		'id'            => $prefix . 'terms',
		'type'          => 'term_ajax_search',
		'multiple-items'=> true,
		'limit'      	=> -1,
		'query_args'	=> array(
			'taxonomy'			=> 'post_tag',
			'hide_empty'		=> false
		)
	) );

}
```

Customize results output
------------------------

[](#customize-results-output)

You can use `cmb_{$field_id}_ajax_search_result_text` to customize the text returned from ajax searches and `cmb_{$field_id}_ajax_search_result_link` to customize the link, check next example:

```
add_filter( 'cmb_your_prefix_demo_posts_ajax_search_result_text', 'cmb2_ajax_search_custom_field_text', 10, 3 );
function cmb2_ajax_search_custom_field_text( $text, $object_id, $object_type ) {
	$text = sprintf( '#%s - %s', $object_id, $text ); // #123 - Post title

	return $text;
}

add_filter( 'cmb_your_prefix_demo_posts_ajax_search_result_link', 'cmb2_ajax_search_custom_field_link', 10, 3 );
function cmb2_ajax_search_custom_field_link( $link, $object_id, $object_type ) {
	if( $object_id == 123 ) {
		$link = '#';
	}

	return $link;
}
```

Retrieve the field value
------------------------

[](#retrieve-the-field-value)

If multiple == false will return the ID of attached object: `get_post_meta( get_the_ID(), 'your_field_id', true );`

If multiple == true will return an array of IDs of attached object: `get_post_meta( get_the_ID(), 'your_field_id', false );`

Changelog
---------

[](#changelog)

### 2.0.0

[](#200)

*Ed-IT Solutions Fork Begins here*

- Installable from composer (this breaks the plugin style code).
- Implements [rubengc#14](https://github.com/rubengc/cmb2-field-ajax-search/pull/14)

### 1.0.2

[](#102)

- Updated devbridgeAutocomplete lib

### 1.0.1

[](#101)

- Group fields support
- Widget area support
- Use of devbridgeAutocomplete() instead of autocomplete() to avoid errors

### 1.0.0

[](#100)

- Initial commit

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2148d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/930b97839168b9f3710451fa89ba02c5f5642d9c33efc77f4d9f5b621a9941be?d=identicon)[Arcath](/maintainers/Arcath)

---

Top Contributors

[![rubengc](https://avatars.githubusercontent.com/u/5462480?v=4)](https://github.com/rubengc "rubengc (10 commits)")[![Arcath](https://avatars.githubusercontent.com/u/19609?v=4)](https://github.com/Arcath "Arcath (4 commits)")[![ko31](https://avatars.githubusercontent.com/u/84167?v=4)](https://github.com/ko31 "ko31 (1 commits)")

### Embed Badge

![Health badge](/badges/ed-itsolutions-cmb2-field-ajax-search/health.svg)

```
[![Health](https://phpackages.com/badges/ed-itsolutions-cmb2-field-ajax-search/health.svg)](https://phpackages.com/packages/ed-itsolutions-cmb2-field-ajax-search)
```

###  Alternatives

[codeconsortium/ccdn-forum-bundle

CCDN Forum Bundle

6015.4k1](/packages/codeconsortium-ccdn-forum-bundle)[samsara/fermat

A library providing math and statistics operations for numbers of arbitrary size.

653.1k3](/packages/samsara-fermat)[gabrielanhaia/laravel-circuit-breaker

Laravel integration for PHP Circuit Breaker — multiple storage drivers, middleware, Artisan commands, and event system

481.0k](/packages/gabrielanhaia-laravel-circuit-breaker)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
