Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Linking result to custom field
- This topic has 3 replies, 2 voices, and was last updated 9 years ago by
Ernest Marcinko.
-
AuthorPosts
-
May 23, 2017 at 5:43 pm #13201
mmmateo
ParticipantHi 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
May 24, 2017 at 11:44 am #13210Ernest Marcinko
KeymasterHi 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.
May 24, 2017 at 4:41 pm #13212mmmateo
ParticipantThanks 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
May 25, 2017 at 9:49 am #13218Ernest Marcinko
KeymasterYou cannot access this content.
-
AuthorPosts
- You must be logged in to reply to this topic.