This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Setting up pre-filters for an index

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Setting up pre-filters for an index Reply To: Setting up pre-filters for an index

#57093
milena_u6jmmilena_u6jm
Participant

Do you think this might work for our needs?

/**
 * Limit ASP index to attachments that meet availability criteria.
 */

// 1. Block indexing on initial save if criteria not met
add_filter(
	'asp_index_on_save_stop',
	function ( $stop, $post_id, $_post, $update ) {
        if ( get_post_type( $post_id ) !== 'attachment' ) {
            return $stop;
        }

        $available  = get_post_meta( $post_id, 'available', true );
        $web_status = get_post_meta( $post_id, 'web_status', true );

        if ( $available != 1 || ! in_array( $web_status, [ 'Available', 'Under Consideration' ], true ) ) {
            return true; // stop indexing
        }

        return $stop;
    },
    10,
    4
);

// 2. When either relevant meta key is updated, check if the item
//    now meets full criteria and re-index (or remove) accordingly.
//    Uses ASP's IndexTable API directly 
add_action(
	'updated_post_meta',
	function ( $meta_id, $post_id, $meta_key ) {
        if ( ! in_array( $meta_key, [ 'available', 'web_status' ], true ) ) {
            return;
        }
        if ( get_post_type( $post_id ) !== 'attachment' ) {
            return;
        }

        $available  = get_post_meta( $post_id, 'available', true );
        $web_status = get_post_meta( $post_id, 'web_status', true );

        $index_table = \WPDRMS\ASP\Hooks\Actions\IndexTable::getInstance();

        if ( $available == 1 && in_array( $web_status, [ 'Available', 'Under Consideration' ], true ) ) {
            // Criteria met — add/update in index
            $index_table->update( $post_id, null, true );
        } else {
            // Criteria no longer met — remove from index
            $index_table->delete( $post_id );
        }
    },
    10,
    3
);
  • This reply was modified 2 months, 3 weeks ago by milena_u6jmmilena_u6jm.