• Hello,

    Is it possible to use an ACF value (url) instead of a permalink for search results?

    For example, I have a custom post type and each has an ACF value for the link. The ACF field is cpt_result.

    I can’t find an option in the plugin settings (though I did find one to use an ACF image for the search result, which was helpful). Is the same thing possible for the permalink?

    Thanks

    • This topic was modified 7 years, 4 months ago by interinactive.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter interinactive

    (@interinactive)

    Surely this is a common request?

    I’d happily purchase the full version if this can somehow work.

    Thanks

    Thread Starter interinactive

    (@interinactive)

    Nevermind, I edited the plugin file (search_current_class.php, which I was trying to avoid):

    $r->link = get_field(‘custom_url’, $v->id);

    Thanks

    Plugin Author wpdreams

    (@wpdreams)

    Hi,

    You could use a filter instead of a direct change. Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

    add_filter( 'asl_results', 'asl_custom_link_meta_results', 10, 1 );
    function asl_custom_link_meta_results( $results ) {
        // Change this variable to whatever meta key you are using
        $key = 'custom_url';
    
        // Parse through each result item
        foreach ($results as $k=>$v) {
            if ( function_exists('get_field') )
                $new_url = get_field( $key, $v->id, true ); // ACF support
            else
                $new_url = get_post_meta( $v->id, $key, true );
    
            // Change only, if the meta is specified
            if ( !empty($new_url) ) {
                $results[$k]->link = $new_url;
            }
        }
    
        return $results;
    }

    Change the $key variable to the custom field name, and that should do the trick.

    Best regards,
    Ernest M.

    Thread Starter interinactive

    (@interinactive)

    Works perfectly, thanks for the update!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Using ACF value instead of permalink’ is closed to new replies.