Home › Forums › ACF PRO › Using location slugs in ACF php export code › Reply To: Using location slugs in ACF php export code
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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.