Support

Account

Forum Replies Created

  • For anyone else arriving here later,
    If you check line 425 in options-page.php you’ll see it’s doing a bit more than that. It seems to be applying filters, checking for parent child redirects, returning false if there are no options pages, and finally returning all the pages.

  • Thanks for the reply. I did end up figuring that out. The documentation is confusing to me on this since in the basic example it does an if get_field() check which can only return true making it seemingly useless to check. I guess there are still cases when the template might not have that field so you would want to check if the group exists but that’s not immediately clear to me from the documentation. My incorrect assumption was that the if statement in the documentation was checking if the group had any values inside of it’s fields. It seems in most cases with ACF “if get_field()” usually returns true after the admin has entered some data, not after the field is created in the ACF ui. Anyway, thanks for the reply!

    <?php
    		
    // vars
    $hero = get_field('hero');	
    
    if( $hero ): ?>
    	<div id="hero">
    		<img src="<?php echo $hero['image']['url']; ?>" alt="<?php echo $hero['image']['alt']; ?>" />
    		<div class="content">
    			<?php echo $hero['caption']; ?>
    			<a href="<?php echo $hero['link']['url']; ?>"><?php echo $hero['link']['title']; ?></a>
    		</div>
    	</div>
    	<style type="text/css">
    		#hero {
    			background: <?php echo $hero['color']; ?>;
    		}
    	</style>
    <?php endif; ?>
  • I’m currently using array_filter to get around this but that seems slower than using the normal methods for checking if a field is set.

  • Here’s how I’ve done it. I haven’t tested uploading every file type but the ones I have tried do work. This is using material design icons.

    
    <?php if( have_rows( 'download_list_clone_download_groups' ) ): ?>
      <section class="download-lists">
        <?php while( have_rows( 'download_list_clone_download_groups' ) ): the_row(); ?>
          <article class="download-list">
            <h3 class="download-list__title"><?php the_sub_field( 'group_title' ); ?></h3>
            <?php if( have_rows( 'downloads' ) ): ?>
              <ul>
                <?php while( have_rows( 'downloads' ) ): the_row(); ?>
                  <?php $attachment_id = get_sub_field('file'); ?>
                  <?php $attachment_url = wp_get_attachment_url($attachment_id); ?>
                  <?php $pathinfo = pathinfo( get_attached_file($attachment_id)); ?>
                  <?php $filetype = $pathinfo['extension']; ?>
                  <?php
                    switch ($filetype) {
                      case "pdf":
                          $fileclass = 'file-pdf';
                          break;
                      case "jpg":
                      case "jpeg":
                      case "png":
                      case "svg":
                          $fileclass = 'file-image';
                          break;
                      case "pptx":
                      case "pptm":
                      case "ppt":
                          $fileclass = 'file-powerpoint';
                          break;
                      case "xlsx":
                      case "xlsm":
                      case "xls":
                          $fileclass = 'file-excel';
                          break;
                      case "doc":
                      case "docm":
                      case "docx":
                      case "odt":
                      case "rtf":
                      case "txt":
                          $fileclass = 'file-word';
                          break;
                      default:
                          $fileclass = 'file-document';
                          break;
                    }
                   ?>
                  <li class="download-list__download"><a href="<?php echo $attachment_url; ?>"><i class="mdi mdi-<?php echo $fileclass; ?>"></i><span><?php the_sub_field( 'download_title' ); ?></span><i class="mdi mdi-download"></i></a></li>
                <?php endwhile; ?>
              </ul>
            <?php endif; ?>
          </article>
        <?php endwhile; ?>
      </section>
    
    <?php endif; ?>
    
  • Hmm, There doesn’t seem to be a row in the postmeta table with the post id or a row with that meta_key. Maybe I should try this all out in a fresh WP install. Is there another place that row could be saved?

  • It definitely seems like a bug to me.

    I gave up and I’m doing this a different way. In my ajax function I’m adding the user to the post and additionally adding the post to a field on the user profile.

    Hope this helps someone else in the future.

  • I removed the ajax altogether to test this another way. I used an empty form submit with a function that is hooked to init that checks posted data and runs if it finds a value.
    I also simplified the function so it only updates the field with the user id.

    function add_volunteer() {
    
      if ( isset( $_POST['add-volunteer'] ) && '1' == $_POST['add-volunteer'] ) {
    
        $cleanup_id = $_POST['cleanup_id'];
        $user_id = get_current_user_id();
    
        update_field('field_57e89bd93b402', $user_id, $cleanup_id);
    
      } 
    
    } 
    
    add_action( 'init', 'add_volunteer' );

    I also tested it with the field set to select multiple values and select single value.

    I’m still seeing the same issue though. Is it possible that this is a bug?

  • This is my ajax function if it helps at all

    function add_user_to_cleanup() {
      $add_remove = $_POST['add_remove'];
      $cleanup_id = $_POST['cleanup_id'];
      $user_id = get_current_user_id();
    
      $volunteers = get_field('field_57e89bd93b402', $cleanup_id, false);
    
      if (empty($volunteers)) {
        $volunteers = array();
      }
    
      if ($add_remove == 'add' && !in_array($user_id, $volunteers)) {
        //if user is not in volunteers array then add them
          array_push($volunteers, $user_id);
      } elseif ($add_remove == 'remove') {
        //find user in array and remove them
        $pos = array_search($user_id, $volunteers);
        unset($volunteers[$pos]);
      }
    
      update_field('field_57e89bd93b402', $volunteers, $cleanup_id);
    }
    add_action( 'wp_ajax_add_user_to_cleanup', 'add_user_to_cleanup' );
    add_action( 'wp_ajax_nopriv_add_user_to_cleanup', 'add_user_to_cleanup' );
  • Hey James,
    Thanks for all the help. If I add the user via ajax this is the table
    a:8:{s:4:"type";s:4:"user";s:12:"instructions";s:0:"";s:8:"required";i:0;s:17:"conditional_logic";i:0;s:7:"wrapper";a:3:{s:5:"width";s:0:"";s:5:"class";s:0:"";s:2:"id";s:0:"";}s:4:"role";s:0:"";s:10:"allow_null";i:0;s:8:"multiple";i:1;}

    If I then press update from the admin area it stays the same but the user is counted.

    I tried it using the field key and the issue persisted.

  • Another interesting thing is if a user adds themselves to the relationship field with the front end button and then I go to the back end and update the post there by just pressing the update button, the user is counted in the reverse relationship query.

    My thought was that the post needs to be updated in the ajax code so I added

    
    update_field('volunteers', $volunteers, $cleanup_id);
    wp_update_post( $cleanup_id );

    $cleanup_id is passed into the ajax function as the post id. Unfortunately this doesn’t seem to update the post.

  • I’ve discovered what’s happening. I have users adding themselves to the relationship field via an ajax button. This works perfectly and the user is added to the field and if I go to the backend to edit that post I see the user in the relationship field. Also if I loop through all the posts the user is found in the relationship field. However, when I do a reverse relationship query, the user is only counted if they are added from the back end and not from my ajax. I’m using update_field();

    Does anyone have an idea why this might be?
    Do I need to call a function to refresh the post after update_field()? or is this a bug?

  • Thanks so much for the help. It turns out the code was working properly from the beginning.. For some reason this query is only grabbing posts that belong to the logged in user. If another user created the post it doesn’t seem to count it. This is definitely related to something else I’ve done without realizing it.

    Thanks again for the awesome help!

  • Hey James, thanks for the reply.
    I tried that just now and no luck unfortunately. Any other ideas?

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