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

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

Viewing 5 posts - 16 through 20 (of 20 total)
  • Author
    Posts
  • #56914
    milena_u6jmmilena_u6jm
    Participant

    Thanks Ernest, that worked a treat!

    Appreciate the effort.

    #56915
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

    #57057
    milena_u6jmmilena_u6jm
    Participant

    Hey Ernest,

    So an update on this one, generally, this is working as expected for existing media items, so thanks!!

    I have modified this to also add a second acf field that also has to be there else no index. What I have noticed though, was some new media items were showing in the index when they didnt have those flags selected. I will add, we add these media items via the rest API, I wondering if there’s some funny timing things happening? I think with the rest scripts, the initial item is added, then we do a second pass to add the acf values, not sure if relevant, but thought I would add that detail. Let me know what you think.

    add_filter(
    	'asp/index/database/get_posts_to_index/query/add_where',
    	function ( $add_where ) {
    		global $wpdb;
    		return $add_where . " AND ( 
    		 ( post.post_type='attachment' AND EXISTS(
    		   SELECT 1 FROM $wpdb->postmeta pm1 
    		   WHERE pm1.post_id=post.ID 
    		   AND pm1.meta_key='available' 
    		   AND pm1.meta_value=1
    		 ) AND EXISTS(
    		   SELECT 1 FROM $wpdb->postmeta pm2 
    		   WHERE pm2.post_id=post.ID 
    		   AND pm2.meta_key='web_status' 
    		   AND pm2.meta_value IN ('Available', 'Under Consideration')
    		 ))
    		 OR post.post_type <> 'attachment'
    		)";
    	}
    );
    #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.
    #57095
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    So sorry, I mixed up some emails again and a few tickets were left behind (including yours).

    I don’t see an obvious issue with the first code. However that only applies whenever the indexing is done via cron/cli or via the back-end with the “Continue/New index” buttons, as applies to the piece of code that gathers the posts to index.

    Your second solution looks clean too, it should work – I would still keep the first code active as well, so the 3 snippets to gether should cover all the cases.

    Very soon there will be a new upgrade 4.28.3, which will have a brand new cache interface with a new caching method: “super file cache” (very fast, almost instant direct file access cache).
    After that I think we are moving towards 4.29.0, where I want to rework the index table interface (including the indexing methods) and also add an option for custom field exclusions – or at least a unified hook for rules if the options turn out to be too complex. That will hopefully replace these snippets, as right now it is just way too much pain in my opinion. I really want to improve the whole indexing thing in general, right now it is way to cluttered for new features.

Viewing 5 posts - 16 through 20 (of 20 total)
  • You must be logged in to reply to this topic.