0

I need to have the value of a custom field in the functions.php file. This what I already have. I can get the value on a page template, but in this case it is need in the functions.php file because I need that value in the facewp function.

add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
    $offer_id = get_field('offer_type'); // need this value from the current page

    $term = get_term( $offer_id, 'offer' ); // offer = tax slug
    $slug = $term->slug;
    
    if ( empty( $url_vars['offer_type'] ) ) {
        $url_vars['offer_type'] = [$slug];
    }
    return $url_vars;
} );

How can I achieve this? Now I'm getting an empty variable.

1 Answer 1

0

Found a solution:

add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
        //get the id of the current page
        $url = 'http://' . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ];
        $current_post_id = url_to_postid( $url );

        //get the offer_type (custom field) from the current page id
        $offer_id = get_field('offer_type', $current_post_id); //returns an id
        
        //get the taxonomy slug from the offer_id
        $term = get_term( $offer_id, $taxonomy );
        $slug = $term->slug;

        //preselect the offer_type facet
        if ( empty( $url_vars['offer_type'] ) ) {
            if($slug) {
                $url_vars['offer_type'] = [$slug];
            }
        }
        return $url_vars;
} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.