0

I am struggling with this problem. I have a group pf ACF fields and a XML file with the corresponding values. What I want is the user to be able to fill one field (with an id) and on the Publish post, to populate the values for the other fields based on the id. I am able to do the searching on the XML and fill the values, but I don't know how to run this function. I tried with 'save_post' hook but it runs the function before the publish and the id field is still empty. Any ideas how to run a function with a value from a custom field and the post id as vars?

I have this code:

function test_read($post_id) {
    $file = glob('wp-content/test_folder/*.{xml}', GLOB_BRACE);
    $new_ar = [];
    foreach($file as $xml){
      push_array($new_ar, $xml->name);
    }
    $value_str = $new_ar[0] .
    update_post_meta($post_id, 'acf_test', $value_str );
 }
 add_action('save_post', 'test_read');

It seems that I am not able even to navigate through the folder. What am I doing wrong? Thank you

0

Untested, but if I understand you correctly, try something like this:

$results = new WP_Query( array (
    'post_id'      =>  '<id-here>',
    'post_type'    => 'acf-field',  //not necessary, but insurance
    'post_excerpt' => '<custom-field-name>', //not necessary, but insurance
    'meta_query'   => array ( 
        'key'      => '<custom-field-name>',
        )
    )
);
5
  • I added some of my code. Actually I want to add meta_data to my post, after saving it. Is it possible? – marissalianam Dec 5 '20 at 22:02
  • 1. odd place for a proprietary folder, I'd stay away from the core. 2. that is an odd construction for a glob. I've never seen .{xml} 3. are your permissions right? – breadwild Dec 5 '20 at 23:15
  • I suppose it;s right because when I run it directly from functions.php I can read the folders. With scandir I am not able to read the folder. Do you think I should put it somewhere else? – marissalianam Dec 6 '20 at 9:50
  • Check out this thread, it might be helpful: wordpress.stackexchange.com/questions/288995 BTW, I think it's not the position of your directly, in retrospect I think wp-content is fine as long as it makes sense semantically. – breadwild Dec 6 '20 at 14:28
  • Unfortunately it didn;t work. It returns and empty array again. – marissalianam Dec 6 '20 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.