Remove attachments from link autosuggest results

add_filter( 'fl_builder_auto_suggest_lookup', function( $data, $action ) {

	if ( 'fl_as_links' === $action ) {
		global $wpdb;

		$data  = array();
		$like  = FLBuilderAutoSuggest::get_like();
		$types = FLBuilderLoop::post_types();
	//	$slugs = array( 'attachment' );

		foreach ( $types as $slug => $type ) {
			$slugs[] = esc_sql( $slug );
		}

		// we can't use an array of arrays for prepare() so use sprintf 1st.
		$query = sprintf( "SELECT ID, post_title, post_type FROM {$wpdb->posts}
			WHERE post_title LIKE %%s
			AND post_type IN ('%s')
			AND post_status IN ('publish', 'inherit')",
			implode( "', '", $slugs )
		);

		// @codingStandardsIgnoreStart
		$posts = $wpdb->get_results( $wpdb->prepare( $query, '%' . esc_sql( $like ) . '%' ) );
		// @codingStandardsIgnoreEnd

		foreach ( $posts as $post ) {

			switch ( $post->post_type ) {
				case 'attachment':
					$name  = basename( wp_get_attachment_url( $post->ID ) );
					$value = wp_get_attachment_url( $post->ID );
					break;
				case 'popup':
					$name  = esc_html( $post->post_title );
					$value = '#popmake-' . $post->ID;
					break;
				default:
					$name  = esc_html( $post->post_title );
					$value = get_permalink( $post->ID );
			}

			$data[] = array(
				'name'  => $name,
				'value' => $value,
				'type'  => ucfirst( $post->post_type ),
				'id'    => $post->ID,
			);
		}
	}
	return $data;
}, 10, 2 );
1 Like

Nice. I’d vote for this being promoted to an Advanced > settings toggle.
It’s the default we’d want to use.