Support

Account

Home Forums Add-ons Flexible Content Field Include flexible fields to site search

Solving

Include flexible fields to site search

  • Hi all,

    I have custom search setup. I have something like few websites on one standart WP, so i’m using gates url value to determinate in which post type to search. Everything works fine when you are searching for page title, however i have no the_content() field for those posts as i’m using flexible field text field. How can i add it to this custom search? In this case i need to include ALL text subfields used in post flexible content.

    SEARCH QUERY:

    
    <?php
    $args = array(
        'post_type'=> $_GET['gates'],
        's' => $_GET['s']
    );
    
    $search = new WP_Query($args);
    
    if ( $search->have_posts() ) : while ( $search->have_posts() ) : $search->the_post(); ?>
    
    RESULTS HERE
    
    <?php endwhile; endif; ?>

    FLEXIBLE FIELD:

    
    <?php if( have_rows('modules') ): ?> while ( have_rows('modules') ) : the_row(); 
    
    if( get_row_layout() == 'photo' ): echo 'photo'; } 
    elseif( get_row_layout() == 'text' ): echo 'text'; } 
    
    <?php endif;  endwhile; ?>
    <?php else : endif; ?>

    Thank you!

  • hope i understand you right. (i had done core search ability by a workaround)
    if you dont use the normal content field of WP, then you could try something like that:

    function my_acf_update_text($post_id) {
    		
    if( have_rows('my_custominhalt') ):
    
         // loop through the rows of data
    	while ( have_rows('my_custominhalt') ) : the_row();
          
    	        $my_search="";
    	        if( get_row_layout() == 'text-block' ){
    	        $my_subtitle = get_sub_field('my_subtitle');        
    	        $my_content = get_sub_field('my_text_content');
    	        $my_search = '<h3>'.$my_subtitle.'</h3><p>'.$my_content.'</p>';
    	        }
    	             
    	        if( get_row_layout() == 'text-image-block' ){
    	        $my_subtitle = get_sub_field('my_subtitle');        
    	        $my_content = get_sub_field('my_imagetext');
    	        $my_search = '<h3>'.$my_subtitle.'</h3><p>'.$my_content.'</p>';       
    		}
    		$my_search_full = $my_search_full.' '.$my_search;
    	endwhile; 
    endif;
    	
    	// update post with the post_content created from 
    	$my_post = array('ID'=> $post_id,'post_content' => $my_search_full);
    
    remove_action('save_post', 'my_acf_update_text');
    wp_update_post( $my_post );
    add_action('save_post', 'my_acf_update_text');
    
    }
    add_action('save_post', 'my_acf_update_text');

    place it into your plugin, or functions.php
    what it does : it create a action that loop through all flexible fields with text, and save them to post_content. thanks to that i am able to search content by core function.

    hope that help you too (of course you need to adjust it to fit your needs)
    be careful, it overwrite anything inside post_content(core-wysiwyg of WP)
    you also need to re-save every post before it works, because only after save content is inserted into post_content

    if you need core-wysiwyg, then create a additional field and save the loop into that field. basically it would be easier to search only inside a single field instead of a repeater/flexible field

  • Thanks mediawerk 🙂 Looks like really good solution!!!

    Meanwhile i’m actually came up with wp query and meta key solution, however not sure what should i do in order to have all modules_%_text as keys (can’t get it working, without adding EACH line of them like 0,1,2…).

    $args = array(
      'post_type'=> 'post',
      'paged'    => $paged,
      'posts_per_page' => 9,
      'meta_query' => array(
      'relation' => 'OR',
       array('key' => 'modules_0_text','value' => $_GET['word'],'compare' => 'LIKE'),
       array('key' => 'modules_1_text','value' => $_GET['word'],'compare' => 'LIKE'),
       array('key' => 'modules_2_text','value' => $_GET['word'],'compare' => 'LIKE'), 
    );
    
    $search = new WP_Query($args);

    Here $_GET[‘word’] is custom url parameter value (instead of s=value)

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

The topic ‘Include flexible fields to site search’ is closed to new replies.