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

PDF search results

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #40679
    ballerinamooseballerinamoose
    Participant

    Hi,

    I am having trouble with a few issues:

    1) I would like the plugin to search PDF files attached to posts in a certain category (Journals). I would like the search results to display ONLY PDFs that are attached to posts in that category (no unattached PDFs, no PDFs attached to posts in other categories or to pages).

    I found this post, which is similar, but it uses a custom post type rather than a category:
    https://wp-dreams.com/forums/topic/media-search-results/

    2) I would also like for the search results to display an excerpt of the PDF text, but link to the parent post. In my current configuration, if I search for the word “test”, I do get the correct behavior in the search dropdown.

    However, if I click through to the search page by hitting enter, my search results link to the attachment directly and not to the parent page. Also, I would like this page to show a excerpt of text from the PDF (I think my theme is causing it not to show, because I have excerpts disabled for the blog archive).

    3) And finally, have you done the release discussed in this post, for creating images from PDFs?
    https://wp-dreams.com/forums/topic/cant-get-good-pretty-pdf-search-results-like-your-demo/

    Thank you very much for your help, and happy new year.

    #40693
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    1. In theory, something like this could do the trick:

    add_filter( 'asp_results', 'asp_attachment_parent_in_category', 10, 1 );
    function asp_attachment_parent_in_category( $results ) {
      // Category IDs
      $categories = array(1, 2, 3);
      
      foreach ($results as $k=>&$r) {
    	if ( $r->content_type == 'attachment' ) {
    		$parent_id = wp_get_post_parent_id( $r->id );
    		if ( !is_wp_error($parent_id) && !empty($parent_id) ) {
    			if ( !in_category($categories, $parent_id) ) {
    				unset($results[$k]);
    			}			
    		}
    	}
      } 
      return $results;
    }

    This only works with categories – no other taxonomy terms or anything else. The $categories variable holds the category IDs for which you want the parents to be restricted, don’t forget to change that.

    2. This may not be possible without actual changes on the theme. The plugin forces the same titles/contents/links for the results page, but usually the results page queries these fields individually and therefore it refreshes them from the database.

    There is still hope though, try this custom code:

    add_filter( 'asp_regular_search_result', 'asp_force_guids', 10, 1 );
    function asp_force_guids( $r ) {
    	if ( isset($r->asp_data) ) {
    		$r->guid = $r->asp_data->link;
    		$r->post_excerpt = $r->asp_data->content;
    		$r->post_content = $r->asp_data->content;
    	}
    	return $r;
    }

    3. Not yet, there are potential security vulerability concerns that has to be resolved to make it safe for release. It will be implemented for sure though.

    #40731
    ballerinamooseballerinamoose
    Participant

    Hi,

    Thanks so much for the reply.

    1. Unfortunately did not work. To test it, I have three PDFs–only one is attached to a post in the desired category, one is attached to a post in another category, and the last is unattached. All three still show up in search.

    2. This partially worked. The results page now shows the snippet, but it still links directly to the PDF and not to the parent post.

    3. Good to hear.

    Thank you!

    #40737
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    1. Try this variation:

    add_filter( 'asp_results', 'asp_attachment_parent_in_category', 10, 1 );
    function asp_attachment_parent_in_category( $results ) {
      // Category IDs
      $categories = array(1, 2, 3);
      
      foreach ($results as $k=>&$r) {
    	if ( $r->content_type == 'attachment' ) {
    		$parent_id = wp_get_post_parent_id( $r->id );
    		if ( !is_wp_error($parent_id) && !empty($parent_id) ) {
    			if ( $parent_id == 0 || !in_category($categories, $parent_id) ) {
    				unset($results[$k]);
    			}			
    		} else {
    			unset($results[$k]);
    		}
    	}
      } 
      return $results;
    }

    2. That is bad news, this may not be possible without theme modifications then. Maybe a bit more aggressive code will do it, try this:

    add_filter( 'asp_regular_search_result', 'asp_force_guids', 10, 1 );
    function asp_force_guids( $r ) {
    	if ( isset($r->asp_data) ) {
    		if ( $r->post_type == 'attachment' ) {
    			$parent_id = wp_get_post_parent_id( $r->id );
    			if ( !is_wp_error($parent_id) ) {
    				$r->guid = get_permalink($parent_id);		
    			}
    		} else {
    			$r->guid = $r->asp_data->link;
    		}
    		$r->post_excerpt = $r->asp_data->content;
    		$r->post_content = $r->asp_data->content;
    	}
    	return $r;
    }
    #40763
    ballerinamooseballerinamoose
    Participant

    Unfortunately, neither of these worked.

    1. This is still showing PDFs from other categories, unattached PDFs, and PDFs attached to pages. I edited the page listed in the private details so it would be easy for you to test & replicate.

    2. This did not change the results, but I am less concerned about this problem. I can always disable the search page and force the search dropdown instead. Unless you have another idea.

    Thank you for your help!

    #40768
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    1.Okay, I have logged in to check and made a tiny modification. Can you please test if this changed anything.

    For reference the new code is:

    add_filter( 'asp_results', 'asp_attachment_parent_in_category', 10, 1 );
    function asp_attachment_parent_in_category( $results ) {
      // Category IDs
      $categories = array(28);
      
      foreach ($results as $k=>&$r) {
    	if ( isset($r->post_type) && $r->post_type == 'attachment' ) {
    		$parent_id = wp_get_post_parent_id( $r->id );
    		if ( !is_wp_error($parent_id) && !empty($parent_id) ) {
    			if ( $parent_id == 0 || !in_category($categories, $parent_id) ) {
    				unset($results[$k]);
    			}			
    		} else {
    			unset($results[$k]);
    		}
    	}
      } 
      return $results;
    }
    #40797
    ballerinamooseballerinamoose
    Participant

    That worked!!! This feature will make a lot of people very happy. 🙂 Thank you so much!

    One more question–and this may not matter because I’m intending to disable the search results page–but now at the top of the search results page it is outputting things like this:

    string(8) “pagepost” int(0) string(8) “pagepost” int(0) string(8) “pagepost” int(10570) string(8) “pagepost” int(10348)
    string(8) “pagepost” int(10348)

    Is that a problem?

    Thank you!

    #40798
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Great 🙂 At least it works now.

    I removed that extra text – I used that for debugging the code, and I forgot to delete that line. I have quickly logged in and corrected that. It should be fine now.

    #40799
    ballerinamooseballerinamoose
    Participant

    Excellent. Thank you so much! I really appreciate all the help! 🙂

    #40800
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

    #40801
    ballerinamooseballerinamoose
    Participant

    You cannot access this content.

Viewing 11 posts - 1 through 11 (of 11 total)
  • The topic ‘PDF search results’ is closed to new replies.