Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Not searching inside elementor templates content › Reply To: Not searching inside elementor templates content
Hi,
Thank you very much for the details.
Interestingly, this may not be possible. I have checked the Index table core, and no matter how I tried to retrieve the content of that post, it was always empty.
– Using WP_Query fails
– Using get_post($post_id) fails
– After some googling, I found a method to fetch the elementor contents directly from that page:
$frontend = new \Elementor\Frontend();
$contentElementor = $frontend->get_builder_content_for_display( $post_id, true );
This returns an empty string as well..
I am not sure if this is an issue with Elementor, or this is how it supposed to work, but there does not seem to be any way of fetching the content of the template pages via the post ID (in this case product ID, but it does not matter). It always returns nothing at all.
The solution would be the code above, but plugging the template ID instead of the post ID. However as far as I know, there is no simple way of getting the single template ID used with the post, or any other way of getting the post content (?).
The only way to bypass this whole issue is to parse the actual HTML output of the missing pages, and then index the required parts. For that, I have added the following code to the functions.php file in your theme directory:
add_filter('asp_post_content_before_tokenize_clear', 'asp_post_content_from_url', 10, 2);
function asp_get_html_from_url( $url ) {
if ( !class_exists('domDocument') )
return false;
$content = file_get_contents($url);
$dom = new domDocument();
$return = '';
if ( function_exists('mb_convert_encoding') )
@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
else
@$dom->loadHTML($content);
$dom->preserveWhiteSpace = false;
$main = $dom->getElementsByTagName('main');
foreach ( $main as $node ) {
$return .= $node->ownerDocument->saveHTML($node);;
}
// Still no image
return $return;
}
function asp_post_content_from_url($content, $the_post) {
if ( $content == '' && !empty(get_post_permalink($the_post->ID)) ) {
$content = asp_get_html_from_url(get_post_permalink($the_post->ID));
var_dump(get_post_permalink($the_post->ID));
}
return $content;
}
add_filter('asp_search_phrase_before_cleaning', 'asp_replace_characters', 10, 1);
add_filter('asp_query_args', 'asp_replace_characters', 10, 1);
function asp_replace_characters( $s ) {
$characters = "',._-?!"; // Type characters one after another
$replace_with = ' '; // Replace them with this (space by default)
if ( is_array($s) ) {
if ( isset($s['s']) && !$s['_ajax_search'] )
$s['s'] = str_replace(str_split($characters), $replace_with, $s['s']);
} else {
$s = str_replace(str_split($characters), $replace_with, $s);
}
return $s;
}
Please keep in mind, that this is not a good solution at all, and it may not work in every case as it should.