Support

Account

Home Forums ACF PRO How can I add a custom location rule for theme hybrid post templates in advance

Helping

How can I add a custom location rule for theme hybrid post templates in advance

  • I am trying to add an Advance Custom Fields custom location rule to choose Theme Hybrid post templates. In my functions.php file I have the following code

    if (!function_exists('acf_location_rules_types')) {
        function acf_location_rules_types( $choices ){
            $choices['Meta Data']['post_template'] = 'Post Template';
    
            return $choices;
        }
    }
    
    if (!function_exists('acf_location_rules_values_post_template')) {
        function acf_location_rules_values_post_template(){
            global $post;
            $templates = get_post_meta( $post->ID, '_wp_post_template', true );
    
            if( $templates ){
                foreach( $templates as $template ){
                    $choices[ $template->meta_id ] = $template->meta_key;
                }
            }
    
            return $choices;
        }
    }
    
    if(!function_exists('acf_location_rules_match_post_template')){
        function acf_location_rules_match_post_template(){
            $current_field = get_post_custom_keys();
            $selected_field = (int) $rule['value'];
    
            if($rule['operator'] == "=="){
                $match = ( $current_field->meta_id == $selected_field );
            }
            elseif($rule['operator'] == "!="){
                $match = ( $current_field->meta_id != $selected_user );
            }
    
            return $match;
        }

    When I try to choose this as a location rule I don’t get any matched values, what parameters am I passing that are wrong?

  • Hi @mojaray2k,

    Thanks for the post.

    Are your values getting retuned when you debug the data?

    I would recommend you begin by debugging the functions line by line to identify where the error is occuring.

    I have also noted you are passing the wrong variable on this line:

    elseif($rule['operator'] == "!="){
                $match = ( $current_field->meta_id != $selected_user );
            }

    It should be:

    elseif($rule['operator'] == "!="){
                $match = ( $current_field->meta_id != $selected_field );
            }
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘How can I add a custom location rule for theme hybrid post templates in advance’ is closed to new replies.