Filter
uncanny-automator
automator_select_custom_post_limit
Filters the maximum number of custom post items that can be selected for an automation.
add_filter( 'automator_select_custom_post_limit', $callback, 10, 1 );
Description
This filter hook allows you to control the maximum number of custom post types that can be retrieved. By default, it's set to 999. Developers can modify this limit based on the specific `$post_type` being queried. Use this hook when you need to fine-tune the number of results displayed in your custom post type selectors.
Usage
add_filter( 'automator_select_custom_post_limit', 'your_function_name', 10, 1 );
Parameters
-
$post_type(mixed) - This parameter is a numeric priority used to determine the order in which this filter hook runs.
Return Value
The filtered value.
Examples
/**
* Example: Limit the number of custom post types returned by the automator_select_custom_post_limit filter.
*
* This function demonstrates how to use the 'automator_select_custom_post_limit' filter
* to conditionally reduce the number of posts fetched when a specific custom post type
* or a certain field ID is detected.
*
* @param int $limit The current post limit, defaulting to 999.
* @param string $post_type The post type being queried.
*
* @return int The modified post limit.
*/
add_filter(
'automator_select_custom_post_limit',
function ( $limit, $post_type ) {
// Define a specific post type for which we want to impose a stricter limit.
$specific_post_type = 'automator_workflow';
// Define a field ID that, if present, should also trigger a limit change.
// This could represent a scenario where this field is meant to select a single item.
$specific_field_id = 'automator_trigger_item_selection';
// Get the current field_id from the POST data.
// Note: In a real-world scenario, $field_id might be passed as a parameter
// to the filter if the context allows. Here, we simulate accessing it.
$current_field_id = isset( $_POST['field_id'] ) ? sanitize_text_field( $_POST['field_id'] ) : '';
// If the post type is the specific one we want to limit, or if the field ID matches,
// reduce the limit to a more manageable number, like 10.
if ( $post_type === $specific_post_type || $current_field_id === $specific_field_id ) {
$limit = 10; // Set a lower limit for this specific case.
}
// Otherwise, if the post type is 'any' (e.g., 'WP_OLD_POST_TYPE' is often used for this),
// we might want to ensure a high limit is still possible if not explicitly limited above.
// However, if the default 999 is already sufficient and not overridden, we don't need
// to do anything here for the general case. The logic above handles exceptions.
return $limit;
},
10, // Priority: 10 is the default.
2 // Accepted arguments: $limit and $post_type.
);
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/wp/helpers/wp-helpers.php:96
public function select_custom_post_func() {
Automator()->utilities->ajax_auth_check();
$fields = array();
if ( ! automator_filter_has_var( 'value', INPUT_POST ) ) {
echo wp_json_encode( $fields );
die();
}
$post_type = automator_filter_input( 'value', INPUT_POST );
$field_id = automator_filter_input( 'field_id', INPUT_POST );
// REVIEW maybe an array of Field IDs or filter to exclude would be better here?
$is_any = 'WP_OLD_POST_TYPE' !== $field_id;
$args = array(
// phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page
'posts_per_page' => apply_filters( 'automator_select_custom_post_limit', 999, $post_type ),
'orderby' => 'title',
'order' => 'ASC',
'post_type' => $post_type,
'post_status' => 'publish',
'suppress_filters' => true,
'fields' => array( 'ids', 'titles' ),
);
$posts_list = Automator()->helpers->recipe->options->wp_query( $args );
if ( ! empty( $posts_list ) ) {
$post_type_label = get_post_type_object( $post_type )->labels->singular_name;
if ( $is_any ) {
$fields[] = array(
'value' => '-1',
// translators: 1: Post type label
'text' => sprintf( esc_html_x( 'Any %s', 'WordPress post type', 'uncanny-automator' ), strtolower( $post_type_label ) ),
);
}
foreach ( $posts_list as $post_id => $title ) {
$post_title = ! empty( $title ) ? $title : sprintf(
// translators: 1: Post ID
esc_attr_x( 'ID: %1$s (no title)', 'WordPress', 'uncanny-automator' ),
$post_id
);
$fields[] = array(
'value' => $post_id,
'text' => $post_title,
);
}
} else {
$post_type_label = 'post';
if ( intval( $post_type ) !== intval( '-1' ) ) {
$post_type_label = get_post_type_object( $post_type )->labels->singular_name;
}
if ( $is_any ) {
$fields[] = array(
'value' => '-1',
// translators: 1: Post type label
'text' => sprintf( esc_html_x( 'Any %s', 'WordPress post type', 'uncanny-automator' ), strtolower( $post_type_label ) ),
);
}
}
echo wp_json_encode( $fields );
die();
}