Support

Account

Forum Replies Created

  • Well – I sort of solved this, but I’m still curious as to how to parse the above string. To get around this I used…

    global $wpdb;
    
    	$query = "
    		SELECT DISTINCT post_name
    		FROM $wpdb->posts
    		WHERE post_title LIKE 'request_status'
    	";
    
    	$result = $wpdb->get_row($query, ARRAY_N);
    	$field_object = get_field_object($result[0]);
    	$status_array = $field_object['choices'];

    this gives an array of choices from the field object instead of trying to read them from the database

  • Maybe that was the versioning was the issue. I think the last version was 5.4.x. I’m working on a system where I don’t have direct control over when plugins get upgraded. Thanks for the clarification.

  • @hube2 Thanks for the link. I’ll try that out and see how it goes. I’m sure I’m missing something really obvious here.

  • After some research and trial and error, here’s what I’ve come up with. For my case it turns out you don’t necessarily need to do anything re the database directly. I used two functions.

    1

    function move_wysiwyg_editor() {
      global $post;
      global $wp_meta_boxes;
      do_meta_boxes(get_current_screen(), 'normal', $post);
      unset($wp_meta_boxes[get_post_type($post)]['normal']);
    }
    add_action('edit_form_after_title', 'move_wysiwyg_editor');

    2

    function remove_plugin_metaboxes() {
      remove_meta_box('id_of_metabox_from_plugin', 'request_form', 'advanced');
      // repeat as needed
    }
    add_action('do_meta_boxes', 'remove_plugin_metaboxes');
  • @hube2 Thanks for the clarification. Now that I’ve checked, those other attributes are working correctly. I’ll try something like the code you’ve linked to.

  • This problem can be solved by doing this…

    
    function pass_post_id_as_string() {
      global $post;
      $post_id = $post-> ID;
      $post_id_as_string = (string)$post_id;
      return $post_id_as_string;
    }
    add_action('wp_head', 'pass_post_id_as_string');
    

    in the location part of the array (see above) set the value as the function.

  • @hube2 That’s exactly what I needed. Thanks very much!

  • @hube2 Thanks for the help! Here’s what I found/did. I’ll double check your code, (because I like it better than mine). In general I’d say it would work. However (not to be argumentative because I really appreciate the help) get_field('time_picker_name', $post_id) will always return true.

    This is because date picker automatically selects the current day (whatever ‘today’ is) even if no other value has been selected. So for instance on an apparently blank field it’ll return today’s date. When a date is selected afterwards, it changes to that date and explicitly populates the field.

    As I said I prefer your code though because it doesn’t rely on checking against a different field like mine does. I needed to do this…

    
    $request_status = get_field('request_status', $post_id);
    
    if ($request_status !== 'Complete') {
      $formatted_date = '';
    } else {
      $date_complete = get_field('date_complete', $post_id);
      $date_completed = new DateTime($raw_date);
      $formatted_date = $date_completed->format('M j Y');
    }
    

    But, I don’t like that I have to rely on my user explicitly setting ‘Complete’ as an option for the select field. I’ll continue to research/work on this.

  • @john Thanks for trying to help! I know it was a bit of a crazy question. Actually I think I (mostly) solved the problem. It’s kind of a hack because I can’t rely on using something like the WP API to GET my data, but my team agrees it’s ok for now.

    The only issue I’m having at the moment related to this is with the date picker field. I need to display a “Date Completed” column in a table. Right now, if I don’t supply a value, it defaults to Jan 18 2017. So even if there’s nothing in the field (I haven’t selected a date), it’s still acting as if a date was supplied.

    At this point, I don’t know if you’d prefer I switch topics (or open a support ticket) though as this may be a separate/off-topic problem. Thanks again!

    Edit –

    My temporary work around is just a quick check on the status field like this…

    
    if ($request_status !== 'Complete') {
      $formatted_date = ''; // the variable I'm passing to my array.
    } else {
      $date = get_field('date_completed', $post_id);
      $date_completed = new DateTime($date);
      $formatted_date = $date_completed->format('M j Y');
    }
    // continue...
    
  • Never mind. I solved it. For anyone interested, my template (in the plugin directory) just does

    <?php
      acf_form_head();
      get_header();
    ?>

    In my plugin file, I made a function like this

    function load_acf_form_head() { // do things to get the template };
    add_filter('wp_enqueue_scripts', 'load_acf_form_head', 0);

    This seems to change the order the template’s called in.

  • @john

    Thanks for the help. I was able to get around having to hardcode acf_form_head() into my theme by doing this (more or less) in the plugin.

    function output_acf_form($args) {
      global $post;
      if (has_shortcode($post->post_content, 'my_shortcode')) {
        acf_form_head();
      }
    // everything else...
    }
  • @james – Thanks. I got it to work late yesterday. In the end, I used the following code. The area where I had to do a little digging was getting the field_group property assigned correctly. After that I set the post_id to 'new' in both files and I’m set.

    Edit – changed the check on $post_id to 'new_post' to clear the form after each use.

    
    function pre_save_request_form($post_id) {
            // check if there's a new request based on post id
            var_export($post_id);
            // 'new' vs 'new_post'
            if ($post_id != 'new_post') {
                    return $post_id;
            }
            if (is_admin()) {
                    return;
            }
            $post = array(
                    // check to see if a 'draft' status can be used.
                    'post_status' => 'draft',
                    // make the title a date w/ timestamp.
                    'post_title' => date("F j, Y, g:i a"),
                    'post_type' => 'request_form'
            );
    
            // insert the posts
            $post_id = wp_insert_post($post);
            // return the new id
    
            return $post_id;
    }
    add_filter('acf/pre_save_post', 'pre_save_request_form');
    
    /*
    Enable a front end ACF form which will generate a new post on submit.
                    */
    
     $fields = get_field_objects();
    $fieldGroup;
    // var_dump($fields);
    foreach ($fields as $field) {
        $fieldGroup = $field['field_group'];
        // var_dump($field, $fieldGroup);
    }
    // var_dump($fieldGroup);
    $options = array(
        'post_id' => 'new_post',
         // field groups has to be an array...
         'field_groups' => array($fieldGroup),
         'submit_value' => 'Start a request',
         'return' => home_url('thank-you')
    );
    acf_form($options); 
  • Ok. I’m a little closer using this code. Still need to be able to create new posts (and/or custom posts?), but at least I solved one of the problems.

    <?php
          $fields = get_field_objects();
          $fieldGroup;
          
          foreach ($fields as $field) {
            $fieldGroup = $field['field_group'];
          }
          
          $options = array(
            'post_id' => 'new_post',
            // field groups has to be an array even though it's just one int...
            'field_groups' => array($fieldGroup),
            'submit_value' => 'Start a request',
          );
    
          acf_form($options);
        ?>
  • hi @James,

    Thanks very much. I can’t believe I did this, but the group I’m working with is using v4 and I was looking at the v5 docs.

    Can you clarify a point for me? In the v4 article of the link you posted and in v4 for acf_form(), there’s an option for field_groups. It should be an array, but I’m not clear on what it’s an array of. Is there a place to find an id for the group?

    Similarly, in the same post, where the code is `function my_pre_save_post($post_id) {
    if ($post_id !== ‘new_post’) {
    return $post_id;
    }
    $title = $_POST[‘fields’][‘field_123456’];
    $post = array(
    ‘post_status’ => ‘draft’,
    ‘post_type’ => ‘post’,
    ‘post_title’ => $title
    );
    $post_id = wp_insert_post($post);
    return $post_id;
    }
    add_filter(‘acf/pre_save_post’, ‘my_pre_save_post’);`
    Where do I get the ‘field_1234556` value from?

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