Support

Account

Forum Replies Created

  • It’s ok, I found the issue, was a code discrepancy
    Apologies

  • Hi @prithsr ,

    No problem. It would go in the functions file.

    I’ve tried to add notes to help but appreciate it may not make sense

    Any questions, let me know

    Cheers

  • Hi @prithsr

    Just one other thought on this.

    You may need to move the date check conditional into the user results loop, this is because I assume the date info is set against the user, therefore, as you then get the user->ID from the user results loop, you can then access those custom fields!

    Cheers

  • Hi @prithsr

    Ok, if I’ve understood right, here’s how I think I’d possibly approach it:

    I’d use the acf/save_post function:

    add_action('acf/save_post', 'my_acf_save_post');
    function my_acf_save_post( $post_id ) {
    
        // Get newly saved values.
        $values = get_fields( $post_id );
    
        // Get post author ID
        $author_id = get_post_field ('post_author', $post_id);
    
        // Add a custom field to the user 
        update_user_meta( $author_id, 'some_custom_field', '1' );
    
    }

    You could then query the users via a php file you run on cron:

    
    	#query our users
    	$args  = array(
    		'role__in'     	=> array('editor'),
    		'role__not_in' 	=> array('administrator'),
    		'exclude'      	=> array(1),	
            'meta_query'        => array(
    			array(
    				'key'       => 'some_custom_field',
    				'value'     => 1
    			)
            )					
    	);	
    	$user_query = new WP_User_Query( $args );
    	$user = get_userdata( $user->ID );
    

    You could wrap the whole query in a date check to compare the current date with the date set by the user

    If the date matches and the query returns a result, you can then reset the custom meta for the user/author:

        // Reset custom field to the user 
        update_user_meta( $user->ID, 'some_custom_field', '0' );

    So roughly the email function could look like:

    ###################################################
    # Send Emails When custom_post_typePublished
    ###################################################
    add_action( 'transition_post_status', 'send_email_notification', 10, 3 );
    function send_email_notification( $new_status, $old_status, $post ) {
    
    	if ( 'publish' !== $new_status or 'publish' === $old_status || 'custom_post_type' !== get_post_type( $post ) )
    	return;
    	
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    	return;	
    	
    	#get the post ID, we can now use this on both publish and update functions
    	$post_id = $post->ID;	
    	
    	#add date check here
    	$todays_date = '';
    	$specific_date  = '';
    	if ($todays_date == $specific_date ) {
    
    		#query our users
    		$args  = array(
    			'role__in'     	=> array('subscriber'),
    			'role__not_in' 	=> array('administrator'),
    			'exclude'      	=> array(1),	
    			'meta_query'        => array(
    				array(
    					'key'       => 'some_custom_field', //only want to send to specific users
    					'value'     => '1'
    				)
    			)					
    		);	
    		$user_query = new WP_User_Query( $args );
    		
    		#get our email data if we have any results
    		if ( ! empty( $user_query->get_results() ) ) {
    			#create an array
    			$email_recipients = array();
    			foreach ( $user_query->get_results() as $user ) {
    				$user = get_userdata( $user->ID );
    					
    				$email_recipients[] = array(
    					'forename'	=> $user->first_name,
    					'email'		=> $user->user_email
    				);	
    				
    				update_user_meta( $user->ID, 'some_custom_field', '0' ); //reset the custom meta until front end form retriggers
    			}
    		}
    		
    		#filter duplicates (may not need this)
    		#$email_recipients = array_unique($email_recipients);	
    		
    		#proceed with the email if we have a result
    		if ($email_recipients) {
    	
    			foreach ($email_recipients as $recipients) {			
    	
    				$to			= $recipients['email'];
    				$subject	= "Email Subject Goes Here";
    				$message	= "<p>Dear {$recipients['forename']}</p>";
    	
    				#create the headers
    				$headers = array(
    					'Content-Type: text/html; charset=UTF-8',
    					#'Cc:' . 'email@domain.com',
    					#'Bcc:' . 'email@domain.com',				
    					'From: '.get_option( 'blogname' ).' <info@domain.co.uk>'
    				);	
    				#send the email
    				wp_mail ($to, $subject, $message, $headers);					
    			}
    		}	
    	
    	}
    
    }

    It may require some spit and polish but should be a rough starting point I hope!?

    Cheers

  • Morning @rudtek

    Thanks for confirming my code worked, that was useful.

    I ended up stripping anything else out my functions file, low and behold, the map displayed.

    I worked through adding the other functions back in until it broke. I then found the below was the reason why it wasn’t working:

    
    ###
    # REMOVE QUERY STRINGS FROM STATIC RESOURCES
    ###
    function cleanup_query_string( $src ){ 
    	$parts = explode( '?', $src ); 
    	return $parts[0]; 
    } 
    add_filter( 'script_loader_src', 'cleanup_query_string', 15, 1 ); 
    add_filter( 'style_loader_src', 'cleanup_query_string', 15, 1 );
    

    It now works!

    Thanks for your time and help!!

  • Thanks

    I still get the same issue

    I’m totally baffled as it’s a blank theme with only a couple of plugins but yet it won’t work!

    Very odd!

  • @rudtek yep, thought it was safer removing it in the forum!

  • Hi @rudtek

    Thanks for the reply. Can’t believe I missed the typo.

    Ok, I’ve amended the typo and tried with true (several ways) but still no joy:

    
    function prefix_enqueue_scripts() {
    
        wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?key=KEY' );   
    	#wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?key=KEY', true );  
    	#wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?key=KEY', '', true );  
        wp_enqueue_script( 'google-map-init', get_template_directory_uri() . '/js/acf-map.js', array('jquery', 'google-maps'), '', true);
    
    }
    add_action( 'wp_enqueue_scripts', 'prefix_enqueue_scripts' );
    

    Checking the console logs, it shows:
    Google Maps JavaScript API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys

    If I check the source code, I can see the script is called but seems to get truncated:
    <script type='text/javascript' src='https://maps.googleapis.com/maps/api/js'></script>
    It’ missing the API key or is that correct?

    Thanks

  • Edit* Sorry, I should also say I’ve tried 5.8.0-beta3 but to no avail
    Thanks

  • Managed to solve this – simply needed to change the priority on the add_action from 1 to 20

  • Thanks John,

    The route I went with (albeit clunky):
    1) Add extra fields to the checkout process (custom meta stored to order post id)
    2) Setup ACF fields and add them to the user (i’m using a role as a rule)
    3) Add a function that checks when we hit the Woo Order thank you page
    4) Use get_post_meta( $order_id, 'custom_field', $single = true); to get the field added at the checkout
    5) Use update_user_meta( $user_id, 'custom_field', $company_name ); to move the info to the user

    Whilst it’s not ideal it does achieve what I need. It also means I’ve a hard copy (so to speak) of the additional data submitted by the customer as it’s part of the order

  • Thanks Dave,

    Yep, that’s what I had. Am assuming it was the reason for the pop up

    At John, so instead of using the form fields,ref by key. Shall I still try form=>false as well?

  • Thanks @john huebner

    I did try that but in the end had to leave this option. Once the main WooCommerce form was triggered, it would then come up with a leave this page error, I assume it’s because I had the ACF form as well

    Many thanks for your reply though

  • Solved it!

    Replace:
    $i = add_row('field_595cec87bddf0', $row, $user_id);
    With:
    $i = add_row('field_595cec87bddf0', $row, 'user_'.$user_id);

    Note the user_ before the ID, otherwise it posts to the postmeta table not the usermeta table!

  • But if it works for a single field why not a repeater (excuse my ignorance)

    Even if I check the DB, not info is added, not even a new empty row

  • Thanks John Huebner

    I’ll take a look although it’s odd as the add_row code works inside the save_post function so not sure why it won’t in the ajax call

  • I’ve switched to:

    			$row = array(
    				'field_5947dbd28c36e'	=> $line[0],
    				'field_5947dbd78c36f'	=> $line[1],
    				'field_5947dbdc8c370'	=> $line[2],
    				'field_5947dbe48c371' => ''
    			);
    			
    			$i = add_row('field_5947dbbd8c36d', $row, $post_id);	

    And hey presto!

    Apologies

  • Apologies, I missed this part:
    $headers = array('From: ' . $user_info->first_name. ' '.$user_info->last_name . ' <'.$user_info->user_email.'>');
    Notice the header is an array!

  • Thanks @acf-support

    I managed to work it out!

    I added:

    
    	$mailResult = false;
    	#$mailResult = wp_mail( 'my@email.com', 'test if mail works', 'hurray' );
    	echo $mailResult;	
    

    I then worked out I had a typo!
    This line:
    $headers = 'From: ' . $user_info->first_name. ' '.$user_info->last_name . ' <' . $user_email .'>' . "\r\n";

    Should be:
    $headers = 'From: ' . $user_info->first_name. ' '.$user_info->last_name . ' <' . $user_info->user_email .'>' . "\r\n";

    Basically, the email didn’t have $user_info-> in front of it!

    Now working

    Cheers

  • Hi blueawesome

    You need to query the database, this article helped me: http://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/

    My code then had something like this:

    				<?php  
    				$last = count($rows);	
    				$i = 1; 				
    				foreach( $rows as $row ) {				
    					preg_match('_([0-9]+)_', $row->meta_key, $matches);	
    					$meta_key = 'team_' . $matches[0] . '_select_staff';
    					$user_id = get_post_meta( $row->post_id, $meta_key, true );									
    				?>

    Once I had the ID of the user, I could then do the following:
    <h5><?php $user_info = get_userdata($user_id); $first_name = $user_info->first_name; $last_name = $user_info->last_name; echo "$first_name $last_name"; ?><?php #echo ' - '.$user_id; ?></h5>
    I’ve no idea exactly what you’re trying to achieve but maybe the above will give you some pointers!

    jeremey’s code is useful but if you’re using a repeater then you need to go the query route I believe

  • Solved it with this:

                      <?php if(get_field('faqs')): while(has_sub_field('faqs')): 				  
                      $variable = get_sub_field('category'); 			  
                      if(get_sub_field('qa')): while(has_sub_field('qa')): ?>
    						  
    					<article class="envor-sorting-item <?php echo $variable; ?>">
    						<header><?php the_sub_field('question'); ?> <i class="fa fa-plus"></i></header>
    						<?php the_sub_field('answer'); ?>
    					</article>
    						  
                      <?php endwhile; endif; 
                      endwhile; endif; ?>

    Not sure what I was doing wrong before!! Hopefully it helps someone else

  • Thanks l.pirondini that really helped!

  • Thanks @elliot for the reply.

    I believe I’ve solved it. I’ve used the below to do what I need, not sure if there is a cleaner way though:

    <ul>
    <?php	
    $args = array( 
    	'post_type' => 'job_sectors',
    	'post_status' => 'publish',
    	'order'    => 'ASC',
    	'orderby'	=> 'name'
    );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    
    ?>			
    	<li><a href="<?php echo get_permalink($pageChild->ID);?>" title="<?php the_title(); ?>"><?php the_title(); ?><?php #the_ID(); ?></a>	
    	<?php
    	global $post;
    	$args = array(
    		'post_type' => 'jobs',
    		'meta_query' => array(
    			array(
    				'key' => 'job_sector',
    				'value' => get_the_ID(),
    				'compare' => 'LIKE'
    			)
    		)
    	);
    	
    	$myposts = get_posts( $args );
    	$total = 0;
    	foreach( $myposts as $post ) : 
    	$total = $total + $post; 
    	?>
    	
    	<?php endforeach; 
    	wp_reset_postdata(); 
    	echo "($total)"; 
    	?>
    	</li>
    
    <?php endwhile; ?>			
    </ul>

    Thanks

  • I use this if it helps@

    
    function custom_field_excerpt() {
    	global $post;
    	$text = get_field('overview');
    	if ( '' != $text ) {
    		$text = strip_shortcodes( $text );
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$excerpt_length = 10; // 20 words
    		$excerpt_more = apply_filters('excerpt_more', ' ' . '...');
    		$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    	}
    	return apply_filters('the_excerpt', $text);
    }
    function custom_field_excerpt_longer() {
    	global $post;
    	$text = get_field('overview');
    	if ( '' != $text ) {
    		$text = strip_shortcodes( $text );
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$excerpt_length = 25; // 20 words
    		$excerpt_more = apply_filters('excerpt_more', ' ' . '...');
    		$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    	}
    	return apply_filters('the_excerpt', $text);
    }

    Just ensure you change this bit to your field name:
    $text = get_field('overview');

    Then to call it I use:
    <?php echo custom_field_excerpt_longer(); ?>

    Cheers

  • Hi,

    Glad to hear you got it working. The above code certainly looks to be on the right lines and clearly returns your result so that must be a good thing.

    The double “” is basically:
    One closing ” as the opening one is on line 2 and the other is the closing ” from line 10

    Cheers

Viewing 25 posts - 1 through 25 (of 49 total)

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.