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

Linking result to custom field

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #13201
    mmmateommmateo
    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.

    [code]
    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;
    }
    [/code]

    This returns [code]field_5903f11905da7[/code], whereas I’d like it to return the URL stored in that field.

    Note that [code]_attach_document[/code] is the name of a custom field I created with Advanced Custom Fields, if that affects anything.

    Thanks for your help,

    Mateo

    #13210
    Ernest MarcinkoErnest 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.

    #13212
    mmmateommmateo
    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.

    [code]
    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;
    }
    [/code]

    Thanks again,

    Mateo

    #13218
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.