Linking result to custom field

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Linking result to custom field

This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 5 years, 1 month ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #13201
    mmmateo
    mmmateo
    Participant

    Hi there,

    I’m loving the plugin so far, but I’ve run up against one issue which seems easy enough, but I can’t seem to figure it out.

    I’m using the search primarily to filter through a custom post type that contains PDFs. Right now, when the user clicks on a result, it takes them to another page where they can click another link to download the PDF. However, I’d like the document to immediately download when they click the link in the search results. I assume I do this by connecting the result output to the ‘document’ custom field, but this is as close as I’ve been able to get.

    
    add_filter( 'asp_results', 'asp_custom_link_meta_results', 1, 1 );
     
    function asp_custom_link_meta_results( $results ) {
     
      // Change this variable to whatever meta key you are using
      $key = '_attach_document';
     
      // Parse through each result item
      foreach ($results as $k=>$v) {
        $new_url = get_post_meta( $v->id, $key, true );
     
        // Change only, if the meta is specified
        if ($new_url != '')
          $results[$k]->link  = $new_url;
      }
     
      return $results;
    }
    

    This returns

    field_5903f11905da7

    , whereas I’d like it to return the URL stored in that field.

    Note that

    _attach_document

    is the name of a custom field I created with Advanced Custom Fields, if that affects anything.

    Thanks for your help,

    Mateo

    Attachments:
    You must be logged in to view attached files.
    #13210
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Mateo,

    Thank you for your kind words!

    It appears that the custom field may not actually contain the document URL but some kind of reference, I’m guessing another custom field name. (?)

    If yes, then this modification may solve the issue:

    This will try to get the custom field value, which is returned from the ‘_attach_document’ field, which is hopefully the URL. Let me know what changes.

    Best,
    Ernest Marcinko

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


    #13212
    mmmateo
    mmmateo
    Participant

    Thanks Ernest,

    That didn’t solve the issue, but it helped point me in the right direction. To solve it, I had to use the ACF function get_field() vs get_post_meta(). This may be helpful to include in your documentation, in case other users are using ACF alongside your plugin.

    
    add_filter( 'asp_results', 'asp_custom_link_meta_results', 1, 1 );
     
    function asp_custom_link_meta_results( $results ) {
     
      // Change this variable to whatever meta key you are using
      $key = 'attach_document';
     
      // Parse through each result item
      foreach ($results as $k=>$v) {
    	$new_url = get_field($key, $v->id, true);
     
        // Change only, if the meta is specified
        if ($new_url != '')
          $results[$k]->link  = $new_url;
      }
     
      return $results;
    }
    

    Thanks again,

    Mateo

    #13218
    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 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.