Filter uncanny-automator

automator_select_posts_by_post_type_limit

Filters the maximum number of posts retrieved for selection by post type within WP-Discuz.

add_filter( 'automator_select_posts_by_post_type_limit', $callback, 10, 1 );

Description

Filters the number of posts retrieved for selection when fetching posts by post type. Developers can use this to control the maximum number of posts displayed, defaulting to 999. Useful for limiting large result sets in dropdowns or lists.


Usage

add_filter( 'automator_select_posts_by_post_type_limit', 'your_function_name', 10, 1 );

Parameters

$post_type (mixed)
This parameter likely represents the current post type being queried or filtered.

Return Value

The filtered value.


Examples

/**
 * Example of how to filter the 'automator_select_posts_by_post_type_limit' hook.
 * This function modifies the 'posts_per_page' argument used when fetching posts
 * for a specific post type. In this example, we're setting a hard limit of 50
 * posts, regardless of the original requested limit, if the post type is 'event'.
 *
 * @param int    $limit     The current posts per page limit.
 * @param string $post_type The post type being queried.
 *
 * @return int The modified posts per page limit.
 */
add_filter(
	'automator_select_posts_by_post_type_limit',
	function ( $limit, $post_type ) {
		// If the post type is 'event', enforce a maximum of 50 posts.
		if ( 'event' === $post_type ) {
			return 50;
		}

		// Otherwise, return the original limit.
		return $limit;
	},
	10, // Priority
	2  // Accepted arguments
);

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-discuz/helpers/wp-discuz-helpers.php:63
src/integrations/wp/helpers/wp-helpers.php:526

public function get_all_posts_by_post_type() {
		// Nonce and post object validation
		Automator()->utilities->ajax_auth_check();
		$options = array();
		if ( ! automator_filter_has_var( 'value', INPUT_POST ) || empty( automator_filter_input( 'value', INPUT_POST ) ) ) {
			echo wp_json_encode( $options );
			die();
		}
		$post_type = automator_filter_input( 'value', INPUT_POST );

		$args       = array(
			// phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page
			'posts_per_page'   => apply_filters( 'automator_select_posts_by_post_type_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, true, esc_html__( 'Any post', 'uncanny-automator' ) );

		foreach ( $posts_list as $post_id => $post_title ) {
			// Check if the post title is defined
			// translators: 1: Post ID
			$post_title = ! empty( $post_title ) ? $post_title : sprintf( esc_html__( 'ID: %1$s (no title)', 'uncanny-automator' ), $post_id );

			$options[] = array(
				'value' => $post_id,
				'text'  => $post_title,
			);
		}

		echo wp_json_encode( $options );
		die();
	}

Scroll to Top