Support

Account

Home Forums General Issues Get field in specific post Javascript API

Solving

Get field in specific post Javascript API

  • I’m trying some things with the JavaScript API. I want to get the value of a field from a specific post, but I could not. Any solution?

    EJ: var field = acf.getField (‘field_123’, post_id);

    // get the field instance
        var field = acf.getField('field_5bfec2d19a884');
        
        // show error if no value
        if( !field.val() ) {
            field.showError('Please add a value');
        }else{
            console.log(field.val());
    
    var post_id = field.val();
            var tipo = acf.getField('field_5bd8cab089158', post_id);
            console.log(tipo);
        }
    
    
  • Same problem here:
    acf.getField('teilnehmer', this.props.selectedPostId, false);

    results in:

    $el: a.fn.init [selector: "", prevObject: a.fn.init(0), context: document]
    cid: "acf28"
    data: {}
    __proto__: acf.Model

    “data” being an empty object.
    so, is this a bug or what did i miss?

  • I found out that it is not possible. The field must be present on the page for this js to work. So at the end it is not doing a request to data, but just reading the dom.

    Which is kind of a joke. I don’t need an api to do a document.querySelector() for me.

    Also it should be mentioned in the docs. Simply cause the term “api” will cause wrong expectations here.

  • API simply means application program interface. The details of what it does are not defined by simply using the term api.

    Reading the documentation it is clear to me that this API is for accessing information on the current page.

    This comes in handy for me because it makes it handy to get values from fields and set values in fields. It also enables me to dynamically alter one field based on a value entered into another field. For example, I can create a select field where the choices are dynamically generated based on the selection in another select field.

    AJAX requests, which is what a solution here would require, is not part of the ACF JS API. For adding AJAX requests I would suggest starting here https://codex.wordpress.org/AJAX_in_Plugins

  • Thx for the wiki lookup on api.
    The details of what it does are not defined by simply using the term api.
    No, you are right. Never the less, a certain expectation is associated with the term. That expectation is formed by the countless api’s out there.

    If you want to use an “api” for syncing inputs… well you found your gem.
    It still does not make sense to me to use and “api” to fetch informations which are already present.

    Also thanks for the link you posted. I appreciate people that point others into the right direction.
    I decided after all to not go with jquery but to use fetch in combination with acf to REST which i can recommend to anybody doing more then syncing inputs.

  • I’ve the same problem. Any solutions?

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

The topic ‘Get field in specific post Javascript API’ is closed to new replies.