Support

Account

Home Forums Add-ons Flexible Content Field Select first row in flexible content

Solved

Select first row in flexible content

  • Hi all,

    I love using ACF and with the Flexible Content addon, it’s allowed me to do so much more with WordPress.

    I have built a system that removes WP default content editor and replaces it with ACF + FC, which then allows the user to create columns for content (one at 100% wide, two at 50%, 4 at 25% etc & they can mix up the order to suit their needs).

    This is great, but because I have all the content stored in ACF, when people do a search in WP it will not echo out the content in the results, as you don’t know what a user has created as they have the ability to mix and match the columns.

    I guess I could use the same ‘if, else’ in the page template, but then this will show every thing when all I really need (I think) is and excerpt of the first column of text.

    So I was wondering if it is possible to loop through the FC fields and get the first field it finds? It looks like you can do it in the repeater, but that is a fixed choice and FC is … well flexible.

    Can anyone help me?

  • Hi @c@Cloud9

    You have 2 options.

    1. Hook into the acf/update_value filter on the flexible content field and use the sub field values to update the post’s the_content value

    2. To find the 1st row, just use some simple PHP like so:

    
    <?php 
    
    $rows = get_field('flexible_content_field_name');
    $content = '';
    
    if( $rows )
    {
    	foreach( $rows as $row )
    	{
    		// debug the row
    		/*
    		echo '<pre>';
    			print_r($row);
    		echo '</pre>';
    		die;
    		*/
    		
    		// set $content
    		$content = $rows['sub_field_name'];
    		
    		
    		//  break the loop
    		break;
    	}
    }
    
    ?>
    
  • Hi Elliot,

    Thanks once again for your help, but I’ve got a couple of questions.

    1) Sorry my PHP skills are still coming along and I’m unsure how would I go about doing that, could you point me in the right direction?

    2) I see you have defined a single sub field name, but It could be from multiple possibilities as it is a flexible content area with more than one layout which have different sub fields. How would I check for this?

    I’ve attached a screen shot of my fields if this helps explain what I have.

    Also reading about the acf/update_value has got me thinking, would it be possible to populate the default WP content editor with the content stored in the ACF fields I have? As this may allow Yoasts SEO plugin to ‘scan’ through the content within ACF without it going through ACF. I just wouldn’t display it on the front-end of the site.

    Just a thought.

    Thanks again.

  • Hi Elliot,

    I have tried looking into getting this to work with no joy.

    Here’s what I have added to the functions file.

    <?php
    add_filter( 'the_content', 'add_to_content' );
    
    function add_to_content($content) {
    		
    		global $post;
    		
    		$rows = get_field('multiple_columns');
    		$content = '';
    		
    		if( $rows )
    		{
    		    foreach( $rows as $row )
    		    {
    		        // debug the row
    		        /*
    		        echo '<pre>';
    		            print_r($row);
    		        echo '</pre>';
    		        die;
    		        */
    		        
    		        // set $content
    		        $content = $rows['one_column'];
    		        // $content .= $rows['two_columns'];
    		        
    		        //  break the loop
    		        break;
    		        
    		    }
    		    
    		}
    		
    	return $content;
    
    }
    ?>

    Can you or anyone else see where I have gone wrong?

    Thanks

  • Hi @Cloud9

    Your code seems fine, but have you done much debugging?

    I can see a commented out debugging script, but you have not mentioned any results from it.

    Also, you are debugging inside the loop, your first action should be to debug the $rows (outside the loop)

    Thanks
    E

  • Hi,

    I’ve gotten close thanks to stumbling onto this post from the old support site http://goo.gl/flryRN

    I’ve had to amended it a little for my needs & it isn’t perfect at all, but maybe we can get to a better solution together?

    You will need to use Relevanssi, with this code added to your functions.php file…

    // Get Relevanssi to display excerpts from your custom fields
    add_filter('relevanssi_excerpt_content', 'excerpt_function', 10, 3); function excerpt_function($content, $post, $query) {
    
    global $wpdb; $fields = $wpdb->get_col("SELECT DISTINCT(meta_key) FROM $wpdb->postmeta");
    
    foreach($fields as $key => $field){ $field_value = get_post_meta($post->ID, $field, TRUE); $content .= ' ' . ( is_array($field_value) ? implode(' ', $field_value) : $field_value ); }
    
    $content = preg_replace($wordlist, '', $content);
    
    return $content; }

    This works, but (and there is a but) it seems to display post revisions, so if you’re like me and use Latin as place holders for dummy content during the build process, it shows up in the results. Also, it seems to show the custom field IDs at times too, depending where your search term shows up in the order of the ACF field content.

    I’ve not figured out a solution to the revisions yet, but to remove the IDs I’ve tweaked the code for this…

    // Get Relevanssi to display excerpts from your custom fields
    add_filter('relevanssi_excerpt_content', 'excerpt_function', 10, 3); function excerpt_function($content, $post, $query) {
    
    global $wpdb; $fields = $wpdb->get_col("SELECT DISTINCT(meta_key) FROM $wpdb->postmeta");
    
    foreach($fields as $key => $field){ $field_value = get_post_meta($post->ID, $field, TRUE); $content .= ' ' . ( is_array($field_value) ? implode(' ', $field_value) : $field_value ); }
    
    // Remove random terms from showing in the search. These are related to the names of the ACF field names
    $wordlist = array('acf_id_1', 'acf_id_2', 'acf_id_3', 'acf_id_4');
    foreach ($wordlist as &$word) {
        $word = '/\b' . preg_quote($word, '/') . '\b/';
    }
    
    $content = preg_replace($wordlist, '', $content);
    
    // The excerpt ready with bits removed from it
    return $content; }

    Again, not ideal, but works of sorts.

  • @ash It seems your code works, but I don’t get how to remove certain items from the results. Could you maybe shed some light on this. I now have the following text in my excerpt that shouldn’t be there.

    …1 content image 355 …1481800527:2 1 content 357 …1481637174:1 1 content three_columns

    These are just some random terms sprinkled to different search results

    and here is what a search result looks like (sorry in dutch)

    …1481637174:1 1 content three_columns Onderwijs Alle kinderen zijn welkom De 26 schoollocaties van Spaarnesant verzorgen openbaar onderwijs, toegankelijk voor alle kinderen op basis van respect voor alle mogelijke…

  • I know this is way late but I just found this for my own purposes and I used your original code (not using Relevanssi). The only change you needed to make was on this line:

    $content = $rows['one_column'];

    change to

    $content = $row['one_column'];

    change $rows to $row

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

The topic ‘Select first row in flexible content’ is closed to new replies.