Support

Account

Home Forums General Issues use get_field in other plugin to get a custom post field

Solved

use get_field in other plugin to get a custom post field

  • Hi Guys,

    I’ve build a seperate plugin for my theme. What i am trying to do is the following: use get_field to retrieve data bound to a custom post type called “cursus”. I will use this data to auto generate a gavrity form.

    public function save_cursus_meta( $ID, $post ){
    		$post_type = get_post_type($ID);
    		$cursus_title = get_the_title($ID);
    		$cursus_start_date = get_field('startdatum', $post->ID);
    		// If this isn't a 'book' post, don't update it.
        if ( "cursussen" != $post_type) return;
    
    		$myPost = get_post($ID);
        if( $myPost->post_modified_gmt == $myPost->post_date_gmt ){
            $this->add_new_form($cursus_title, $cursus_start_date);
        }else{
            return;
        }
     }

    so this line is trying to get the data
    $cursus_start_date = get_field('startdatum', $post->ID);

    Do i need to install ACF within this plugin to get it working? I’ve no clue what i am doing wrong.

    Greetz,

  • ACF will need to be installed and active to use your plugin that uses ACF. There are couple ways to handle this. You can tell people that ACF is required or you can install ACF as part of your plugin https://www.advancedcustomfields.com/resources/including-acf-in-a-plugin-theme/

  • Hi there,

    Thx for the reply.. ACF is already running as a plugin within my theme. The problem is that this function is getting called before ACF is initialized. I could use get_post_meta to work around this..

    but the date format which i am trying to get is saved like this in the database 20170723 and this makes it hard to transfer it back to the correct date format.. You have any advice?

  • You would need to use php date functions to convert the date to what you need.

    If you’re function is being called too early then what you need to do is have it called later. Is this a filter/action running on some WP hook?

  • This is the hook connected to it:

    
    $this->loader->add_action( 'publish_cursussen', $plugin_admin, 'save_cursus_meta', 10, 2);

    The problem is that the ACF data is not yet saved. Is there an better hook for this?

  • I don’t know what that hook is or when it is called. If you want to run your function after ACF has saved values then you should be using acf/save_post https://www.advancedcustomfields.com/resources/acf-save_post/

  • John,

    Thank for the advice.. i’ve hooked it up to acf_post and now it’s working!

    Bye 🙂

  • For anyone searching…

    Although in most cases it’s probably better to include ACF in your plugin, you might have a situation where you know for a fact ACF is already present and you just want access to its functions.

    In your plugin, you can run your kickoff function on acf/init like so:

    add_action( 'acf/init', 'run_my_plugin' );

    That will ensure your plugin runs after ACF has loaded, so you will have access to ACF’s functions.

    Docs: https://www.advancedcustomfields.com/resources/acf-init/

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

The topic ‘use get_field in other plugin to get a custom post field’ is closed to new replies.