• Resolved Konstantin Agafonov

    (@konstantin1agafonov)


    Hello!

    In docs https://fulltextsearch.org/documentation/#item4_2
    there is the following example code:

    /**
     * This snippet adds metadata of "employee" post to search index
     */ 
    add_filter('wpfts_index_post', function($index, $post)
    { 
        global $wpdb;
    
        // Basic tokens 
        /* 
         * This piece of code was commented out intentionally to display things
         * which was already done before in the caller code
        $index['post_title'] = $post->post_title;
        $index['post_content'] = strip_tags($post->post_content);
        */
        if ($post->post_type == 'employee') {
            // Adding new token "employee_data" specially for posts of type "employee"
            $data = array(); 
            $data[] = get_post_meta($post->ID, 'address', true);
            $data[] = get_post_meta($post->ID, 'phone', true); 
            $data[] = get_post_meta($post->ID, 'name', true);
            $data[] = get_post_meta($post->ID, 'function', true);
            $index['employee_data'] = implode(' ', $data);
        } 
        return $index; 
    }, 3, 2);

    Why we need global $wpdb here?

    On our website we have many acf field groups connected to gutenberg blocks(up to 100 field groups). It is very inconvinient to add them manually by code. Can you please advise any other variant of adding big number of fields?

    One more question: where I can find docs for plugin API?

    Thank you!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Epsiloncool

    (@epsiloncool)

    Hi, @konstantin1agafonov

    The list of fields should be available somewhere. Basically you can create an array of field names, right, and iterate over it. But there should be smarter way like getting a list of fields directly from ACF, right?

    How your ACF group looks like? Does it linked with specific post_type? I would read ACF docs to get an idea how to get field list based on post_type.

    Well, the global $wpdb; was left there as an example. Just in case you want to extract some data from database.

    Plugin Author Epsiloncool

    (@epsiloncool)

    The API docs in the development right now.

    Thread Starter Konstantin Agafonov

    (@konstantin1agafonov)

    Regarding acf fields. Since their values all go to page content I decided to give the whole post content to the index in the hook wpfts_index_post.

    But then I realized that page content should go to index by default. I have pages built in Gutenberg with custom blocks. Acf fields are connected to gutenberg blocks and there is some content in the pages at the frontend. However I ran usual indexing and I’m testing a page indexing in the plugin interface and it gives me empty post_content cluster.

    If I do so:

    /**
     * Add page content to search index
     */
    add_filter('wpfts_index_post', function($index, $post)
    {
    	if ( $post->post_type == 'page' ) {
    		$page_content = strip_tags(
    			apply_filters( 'the_content', get_the_content( post: $post->ID ) )
    		);
    		if ( ! empty( $page_content ) ) {
    			$index['page_content'] = $page_content;
    		}
    	}
    	return $index;
    }, 3, 2);

    …then the test index page gives non-empty post_content cluster cluster.

    Also I’ve a question concerning Polylang compatibility. Should I ask it here or start a new topic?

    Thread Starter Konstantin Agafonov

    (@konstantin1agafonov)

    UPD ^^^ post_content cluster stays empty
    but page_content cluster is added and it has right value

    My wp version is 5.8.3

    Plugin Author Epsiloncool

    (@epsiloncool)

    Hi @konstantin1agafonov

    Basically ‘post_content’ should be filled by rendered page content (including shortcodes and Gutenberg blocks), since WPFTS puts “get_the_content” result there by default. Did you try to comment out the wpfts_index_post hook at all? Notice, you should have “Index Shortcode Content” checkbox ON in WPFTS Settings.

    For Polylang – yes, I think it’s better to create a new forum thread.

    Thanks!

    Thread Starter Konstantin Agafonov

    (@konstantin1agafonov)

    “Index Shortcode Content” checkbox is checked. Test post_content cluster for pages with custom Gutenderg blocks is empty.

    Can it be due to Polylang plugin installed?

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Adding big number of custom fields to wpfts_index_post hook. API docs.’ is closed to new replies.