Support

Account

Forum Replies Created

  • I changed the field type to post_object with multiple values enabled for aesthetic reasons. A lot of trial and error and digging found that this works to dynamically change the selected values…

    var selector = '#acf-field_5f1f49df49567-field_5f33ff4c31a81';	
    $(selector)
    .empty() //empty select
    .append($("<option/>") //add option tag in select
        .val("2514335") //set value for option to post it
        .text("Red")) //set a text for show in select
    .append($("<option/>") //add option tag in select
        .val("2514356") //set value for option to post it
        .text("Blue")) //set a text for show in select
    .val(["2514335","2514356"]) //select option of select2
    .trigger("change"); //apply to select2
    
  • Also, the color shows when you select an option – but not after saving…

    http://screencast.com/t/tJbpdXD7UkZG

    Need to figure out as well how to get them to show there.

    Thanks 🙂

  • Any ideas? Keep running into this.

  • Another concept we’ve used actually is a bit of an internal caching system. Something like…

    // Save People + Build cached json data
    function my_save_people( $post_id ) {
    	if ( get_post_type($post_id) == 'person') {
    	
    		// Get all and pack data
    		$data = array();
    		$args = array( 'post_type' => 'person', 'posts_per_page' => -1, 'post_status' => 'publish' );
    		$the_query = new WP_Query( $args );
    		if ( $the_query->have_posts() ) {
    			while ( $the_query->have_posts() ) {
    				$the_query->the_post();
    				$array = get_fields(get_the_ID());
    				$array['name'] = get_the_title();
    				$array['permalink'] = get_the_permalink();
    				$data[] = $array;
    			}
    		}
    		wp_reset_postdata();	
    
    		// Save to cache
    		my_save_cache('people.json', $data);
    	}
    }
    add_action('acf/save_post', 'my_save_people', 20);
    
    // Save Cache
    function my_save_cache($file, $data) {
    	file_put_contents(SOME_PATH . '/cache/' . $file, json_encode($data));
    }
    
    // Get Cache item
    function my_get_cache($file) {
    	return json_decode(file_get_contents(SOME_PATH . '/cache/' . $file));
    }
    

    This puts most of the processing on the admin user when a ‘person’ is added/edited, then writes all the data to a text file which can then be pulled back in as a PHP array. No db hits at all this way for regular visitors.

  • There might be some reason this is a bad idea, but what we’ve done when needing a big data pull is something like below. Performance is much better than get_fields or looping through many records with many get_field() calls inside.

    Maybe a start of a concept that could be incorporated into the plugin somehow.

    // - PEOPLE
    function get_people() {
    	
    	$people = array(); $person_fields = ''; $person_slug = 'person';
    	
    	// Fields
    	$fields = array('first_name', 'middle_name', 'last_name', 'suffix', 'email', 'phone', 'fax', 'linkedin', 'twitter', 'facebook', 'image', 'excerpt');
    		
    	// Build pull fields
    	foreach ($fields as $f) {
    		$person_fields .= "
    		MAX( CASE WHEN wp_postmeta.meta_key = '".$f."'
    		THEN wp_postmeta.meta_value
    		END ) AS '".$f."',";
    	}
    	
    	// Query
    	$sql = "SELECT * FROM (
    		SELECT 
    			wp_posts.ID, 
    			wp_posts.menu_order, 
    			wp_posts.post_title AS 'post_title',
    			wp_posts.post_name AS 'post_name',
    			".$person_fields."
    			concat('".get_bloginfo('url')."/".$person_slug."/',wp_posts.post_name,'/') AS 'permalink'
    		FROM wp_posts
    		LEFT JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
    		WHERE wp_posts.post_status = 'publish'
    		AND wp_posts.post_type = 'person'
    		GROUP BY wp_posts.ID
    		ORDER BY menu_order asc, last_name asc, first_name asc		
    	) AS t WHERE 1=1";
    	$results = $wpdb->get_results($sql,'ARRAY_A'); 
    
    	// Loop
    	foreach ($results as $r) {
    
    		// Something Special
    		$full_name = $r['first_name'];
    		if (!empty($r['middle_name'])) { $full_name .= " " . trim($r['middle_name']); }
    		if (!empty($r['last_name'])) { $full_name .= " " . trim($r['last_name']); }
    		if (!empty($r['suffix'])) { $full_name .= ", " . trim($r['suffix']); }
    		$r['full_name'] = trim($full_name);	
    				
    		// Package
    		$people[$r['ID']] = $r;
    	}
    	
    	return $people;
    }
    

    Hope it helps!

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