Home Forums ACF PRO Using location slugs in ACF php export code


Using location slugs in ACF php export code

  • Hi all,

    I am currently trying to get the ACF working on a multi site. So I have read and implemented the export function and placed the code in the functions.php. Additional I added tbe blog id in the key and name fields.

    When a user/admin adds a multi site, 3 (or more) default pages are created. I need the ACF to be on these pages. Is there a way to add the ACF to these pages without using the ID’s of these pages, but their slugs? (users cannot change the page slugs). The code below inserts the ACF on the page post types with the id’s 4 and 5. Can I change this to slug ‘page-1’ and ‘page-2’?

    'location' => array (
    			array (
    				array (
    					'param' => 'page',
    					'operator' => '==',
    					'value' => '4',
    				array (
    					'param' => 'page',
    					'operator' => '==',
    					'value' => '5',
  • Wait, now that I’m thinking about this (should have done this earliers, but is just popped up :-)) I can use a WP function to return the post id by using the slug!

    function get_ID_by_slug($page_slug) {
    	$page = get_page_by_path($page_slug);
    	if ($page) {
    		return $page->ID;
    	else {
    		return null;
  • Are you still looking for help with this or have you worked it out using the code in your follow up comment?

  • I have this very same problem, but am not certain how skybox’s posted code helps solve the issue.

    Any tips?

  • The way I would do this is, when adding the posts and/or pages to the new site I would record the page ID values that are added in an option value for the site. I would register the field groups in PHP and I’d get the option value and use this to set the post id in the acf field group’s location rules.

  • Ahhh yes, that would be a decent workaround. Thanks for your reply! This approach will definitely come in handy down the line.

    Right now my child sites require many preset options too, so I’m getting around both issues by creating site “templates” and using a cloning plugin. This option ensures all page IDs remain consistent.

  • 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() {

    I hope it should be helpful for someone.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.