Support

Account

Forum Replies Created

  • I just developed this feature for my company, this is the solution (MIT license, as is)

    Create this class within a file in your project, f.i. PageSlug_Location.php

    
    if( ! defined( 'ABSPATH' ) ) exit;
    
    class PageSlug_Location extends \ACF_Location {
    
    	public function initialize() {
            $this->name = 'page_slug';
            $this->category = 'page';
    		$this->label = __( 'Page by slug' );
        }
        
    	public function match( $rule, $screen, $field_group ) {
            
            if ( empty( $screen['post_type'] ) || $screen['post_type'] !== "page" ) {
                return false;
            }
    
            $page = get_post( $screen['post_id'] );
            
            return $rule['operator'] == "==" xor $rule['value'] !== $page->post_name;
        }
        
        public function get_values( $rule ) {
            $choices = array();
        
            $posts = get_posts( array(
                "post_type" => "page",
                "post_status" => "publish",
                "posts_per_page" => -1
            ));
    
            foreach( $posts as $post ) {
                $choices[ $post->post_name ] = $post->post_title;
            }
            
            return $choices;
        }
    }
    acf_register_location_type( PageSlug_Location::class );
    

    Then require the file at ACF init hook, put this in functions.php:

    
    add_action('acf/init', function() {
        require_once('PageSlug_Location.php');
    });
    

    I hope it should be helpful for someone.

Viewing 1 post (of 1 total)