-
Notifications
You must be signed in to change notification settings - Fork 8
Adding a filter to the Object field
You can also change the available filters by using the uf.object.filters
filter. It receives an existing array of filters and the actual field as parameters. The format of the filters array looks like this:
array(
__( 'Type' ) => array(
'type:post' => 'Post',
'type:page' => 'Page',
'type:user' => 'User'
),
__( 'Category' ) => array(
'post-term:4' => 'Ultimate Fields',
'post-term:1' => 'Uncategorized'
)
)
Filters are grouped by labels. Also, the key for each option includes both the type of option it is, as well as the actual value.
We will add an additional "Photographer" filter, which loads options from a custom post type. Later we will actually apply the filter.
<?php
/**
* Parameters for the filter:
*
* @param mixed[] $filters The filters, which are already in place.
* @param UF\Field\Object $field The field whose filters are being modified.
* @return mixed[]
*/
function my_change_object_filters( $filters, $field ) {
// Check for a particular field first
if( 'album' != $field->get_name() ) {
return $filters;
}
// Load the photographers
$photographers = array();
foreach( get_posts( 'post_type=photographer&posts_per_page=-1' ) as $photohrapher ) {
$photographers[ 'photographer:' . $photographer->ID ] = $photographer->post_title;
}
$filters[ __( 'Photographer' ) ] = $photographers;
return $filters;
}
add_filter( 'uf.object.filters', 'my_change_object_filters', 10, 2 );
Doing this will add a new "Photographer" group to the filter dropdown. This however is all it does, meaning that the filter will not applied automatically - you have to do this yourself.
For this you need the uf.object.<type_slug>.args
filter with <type_slug>
being replaced by the actual data type that you want to alter, ex. posts
.
It receives 4 parameters:
/**
* @param mixed[] $args The arguments that are already prepared.
* @param UF\Field\Object $field The field that is loading data.
* @param mixed[] $selection The filters, search and pagination that are applied.
* @param UF\Helper\Object\Type $type The type of items that is being loaded.
*/
In this example, we'd like to use the photographer filter to sort by the photographer
meta field of posts. The code for this looks like this:
function apply_photographers_filter( $args, $field, $filters, $type ) {
// Check for the field
if( 'album' != $field->get_name() ) {
return $args;
}
if( isset( $filters['filters'] ) && isset( $filters['filters']['photographer'] ) ) {
$photographers = $filters['filters']['photographer'];
$args[ 'meta_query' ] = array(
array(
'key' => 'photographer',
'value' => $photographers,
'compare' => 'IN'
)
);
}
// NB: Because of the alien "photographer" filter, the Posts type would think that
// you are trying to select another type and will mark itself as "impossible".
$type->impossible = false;
return $args;
}
add_filter( 'uf.object.posts.args', 'apply_photographers_filter', 10, 4 );
Quick start
- Creating fields and using their values
- Installation
- Administration interface
- Using the PHP API
- Container Settings
Locations
- Overview & Usage
- Post Type
- Options Page
- Taxonomy
- Comment
- User
- Widget
- Shortcode
- Menu Item
- Attachment
- Customizer
Fields
- Fields
- Text
- Textarea
- WYSIWYG
- Password
- Checkbox
- Select
- Multiselect
- Image Select
- File
- Image
- Audio
- Video
- Gallery
- WP Object
- WP Objects
- Link
- Date
- DateTime
- Time
- Color
- Font
- Icon
- Map
- Embed
- Number
- Sidebar
- Complex
- Repeater
- Layout
- Section
- Tab
- Message
Features
- Adding fields to the Customizer
- Conditional Logic
- Front-End Forms
- Administration columns
- Import and Export
- REST API
- JSON Synchronization
- Yoast SEO
Ultimate Post Types
Functions and API
Tutorials