Show document related posts

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Show document related posts

This topic contains 19 replies, has 2 voices, and was last updated by Peter Sippel Peter Sippel 2 months, 3 weeks ago.

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #39653
    Peter Sippel
    Peter Sippel
    Participant

    Hello.
    I want to achieve the following:

    1. User adds a document to the post (will result in a link to the doc). The document contains the word “MySearchTest”
    2. AjaxSearchPro finds the document, knows the file url/name of the document
    3. AjaxSearchPro finds the post where the document was added (as a link)
    4. This post will be in the results, just as some other posts who have “MySearchText” in the post content

    From a former inquiry I had this reply:
    “However it is possible via some minor custom code snippet to replace the document in the results with the post. In case you purchase, you can open a support ticket with this and we will suggest a possible modification.”

    Please advise accordingly.
    Thanks. Peter

    #39654
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Peter,

    Thank you for contacting us!

    First, you need to enabled and test the search for media file contents. Here is the full documentation on how you can do that.
    Once you get that going, and the media results are appearing in the live results list, then apply the custom code below.

    Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

    add_filter('asp_results', 'asp_return_posts_by_attachment_results');
    function asp_return_posts_by_attachment_results($results) {
    	$ids = array();
    	$parent_ids = array();
    	$new_results = array();
    	// Gather existing post results first
    	foreach($results as $k=>&$r){
    		if ( isset($r->post_type) ) {
    			if ( $r->post_type == 'attachment' ) {
    				$parent_ids[] = wp_get_post_parent_id($r->id);
    				unset($results[$k]);
    			} else {
    				$ids[] = $r->id;
    			}
    		}
    	}
    	$parent_ids = array_unique(array_diff($parent_ids, $ids, array(0)));
    	foreach ( $parent_ids as $id ) {
    		$res = new stdClass();
    		$res->id = $id;
    		$res->title = get_the_title($id);
    		$res->content = get_the_content(null, false, $id);
    		$res->image = \WPDRMS\ASP\Utils\Post::parseImage($res, array(
    				'get_content' => false,
    				'image_sources' => array('featured', 'content'),
    				'apply_the_content' => true)
    		);
    		$res->content = wd_substr_at_word(strip_tags($res->content), 130);
    		$res->post_type = get_post_type($id);
    		$res->link = get_permalink( $id );
    		$res->content_type = 'pagepost';
    		$new_results[] = $res;
    	}
    	return array_merge($new_results, $results);
    }

    This custom code removes the attachments, and replaces them with the posts where they were originally attached to.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39660
    Peter Sippel
    Peter Sippel
    Participant

    Hi Ernest,

    thanks for the code, but it’s not working.

    1. When I click on the item in the found box, I’m routed directly to the file post
    2. When I click on the magnifyer to see the results page, I get a critical error

    Please advise.
    Thanks. Peter

    #39668
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    I may have to take a look at this directly, it is working on our test servers unfortunately.

    Can you please add temporary FTP and back-end access? Thank you!

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39675
    Peter Sippel
    Peter Sippel
    Participant
    You cannot access this content.
    #39684
    Peter Sippel
    Peter Sippel
    Participant
    You cannot access this content.
    #39690
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39691
    Peter Sippel
    Peter Sippel
    Participant
    You cannot access this content.
    #39713
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Thank you!

    I made a slight correction to the code, now it seems to be working. I tried to search for a few documents, like “API-desc-guidelines2411” and it seems to be returning only the post now.

    For reference, the updated code is:

    add_filter('asp_results', 'asp_return_posts_by_attachment_results');
    function asp_return_posts_by_attachment_results($results) {
    	$ids = array();
    	$parent_ids = array();
    	$new_results = array();
    	// Gather existing post results first
    	foreach($results as $k=>&$r){
    		if ( isset($r->post_type) ) {
    			if ( $r->post_type == 'attachment' ) {
    				$parent_ids[] = wp_get_post_parent_id($r->id);
    				unset($results[$k]);
    			} else {
    				$ids[] = $r->id;
    			}
    		}
    	}
    	$parent_ids = array_unique(array_diff($parent_ids, $ids, array(0)));
    	foreach ( $parent_ids as $id ) {
    		$res = new stdClass();
    		$res->id = $id;
    		$res->title = get_the_title($id);
    		$res->content = get_the_content(null, false, $id);
    		$res->image = get_the_post_thumbnail_url($id);
    		$res->content = wd_substr_at_word(strip_tags($res->content), 130);
    		$res->post_type = get_post_type($id);
    		$res->link = get_permalink( $id );
    		$res->content_type = 'pagepost';
    		$new_results[] = $res;
    	}
    	return array_merge($new_results, $results);
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39715
    Peter Sippel
    Peter Sippel
    Participant
    You cannot access this content.
    #39719
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Thanks!
    The snippet was added incorrectly, it was only allowed to execute on the site front-end, but the search requests are made on the back-end. Changing the snippet to execute everywhere seems to have resolved the issue.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39720
    Peter Sippel
    Peter Sippel
    Participant
    You cannot access this content.
    #39726
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Yes, I think so – it has to be added via the code, as those elements are added programmatically, and some of the options will not apply to them. Please change the custom code to this:

    add_filter('asp_results', 'asp_return_posts_by_attachment_results');
    function asp_return_posts_by_attachment_results($results) {
    	$ids = array();
    	$parent_ids = array();
    	$new_results = array();
    	// Gather existing post results first
    	foreach($results as $k=>&$r){
    		if ( isset($r->post_type) ) {
    			if ( $r->post_type == 'attachment' ) {
    				$parent_ids[] = wp_get_post_parent_id($r->id);
    				unset($results[$k]);
    			} else {
    				$ids[] = $r->id;
    			}
    		}
    	}
    	$parent_ids = array_unique(array_diff($parent_ids, $ids, array(0)));
    	foreach ( $parent_ids as $id ) {
    		$res = new stdClass();
    		$res->id = $id;
    		$res->title = get_the_title($id);
    		$res->content = get_the_content(null, false, $id);
    		$res->image = get_the_post_thumbnail_url($id);
    		$res->content = wd_substr_at_word(strip_tags($res->content), 130);
    		$res->post_type = get_post_type($id);
    		$res->link = get_permalink( $id );
    		$res->content_type = 'pagepost';
    		$res->date = @date_i18n(get_option('date_format', "Y-m-d") . " " . get_option('time_format', "H:i:s"), get_post_time('U', false, $id));
    		$res->author = get_the_author_meta( 'display_name' , get_post_field ('post_author', $id));
    		$new_results[] = $res;
    	}
    	return array_merge($new_results, $results);
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #39734
    Peter Sippel
    Peter Sippel
    Participant

    Perfect ! Thank you so much !

    #39737
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 15 posts - 1 through 15 (of 20 total)

You must be logged in to reply to this topic.