Support

Account

Home Forums Add-ons Repeater Field Showing ONLY repeater rows that contain a certain keyword

Solved

Showing ONLY repeater rows that contain a certain keyword

  • The title explains it all. Instead of showing every row for a post I’d like to display on the row that contains the search keyword. I’ve considered using the meta in the query but not quite sure how that would work.

  • Hi @esparks

    I believe you can use a parameter in the URL as the search string and then use the $_GET method to get that search string. After that, you can check if the subfield contains that string or not by using strpos() function. Maybe something like this:

    // check if the repeater field has rows of data
    if( have_rows('repeater_field_name') ):
    
     	// loop through the rows of data
        while ( have_rows('repeater_field_name') ) : the_row();
    
    		if (strpos(get_sub_field('sub_field_name'), $_GET["urlparameter"]) !== false) {
    			// display a sub field value
    			the_sub_field('sub_field_name');
    		}
    
        endwhile;
    
    else :
    
        // no rows found
    
    endif;

    Where ‘sub_field_name’ is the subfield name of your repeater and ‘urlparameter’ is the search string parameter in the URL.

    I hope this makes sense. Thanks!

  • I knew it could be that easy. Worked perfectly! Now the trick is how to handle multiple search terms.

  • Hi @esparks

    Querying by multiple strings could be tricky. You can divide the search string using the “+” sign and use the explode() function to get it as an array. after that, you can use the preg_match() function to search the string. This thread should give you more idea about it: http://stackoverflow.com/questions/15862361/check-if-a-string-contain-multiple-specific-words. Please consult to the PHP community for further support regarding this issue.

    Thanks!

  • Hi James @acf-support,

    I can’t seem to get my search query to work for repeater rows. This looked like the solution but I feel stuck.

    I have a search results page that should show all regular posts and a repeater from within a custom post type. The CPT is ‘troubleshooting’ and inside each post from this CPT there is a repeater (ts_article) which has a ‘title’ and ‘content’. The results page should show the regular posts as well as the repeater rows that have the search query in it.

    Here’s a sample code of when a custom post type (troubleshooting) is found.

    <?php } elseif ($type == 'troubleshooting') { ?>
    <?php if( have_rows('ts_article') ): ?>
    <?php while ( have_rows('ts_article') ) : the_row(); ?>
    <!-- article -->
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
      <div class="results-list">
    
        <a href="<?php the_permalink(); ?>">
          <p class="result-category">
            <?php $cats=get_the_category(); ?>
            <?php echo $cats[0]->cat_name; ?> - <?php the_title(); ?>
          </p>
          <h3><?php echo the_sub_field('title'); ?></h3>
          <?php if( get_sub_field('content') ): ?>
          <div class="result-excerpt">
            <?php echo custom_field_excerpt(); ?>
          </div>
          <?php endif; ?>
        </a>
    
      </div>
    
    </article>
    <!-- /article -->
    <?php endwhile; ?>
    <?php endif; ?>
    <?php } ?>
  • Hi @bsebastian86

    I’m not sure how the search page works, but I believe you should be able to do it like this:

    if ( (strpos(get_sub_field('title'), $_GET["s"]) !== false) || (strpos(get_sub_field('content'), $_GET["s"]) !== false) ) {
        // display a sub field value
        the_sub_field('title');
        the_sub_field('content');
    }

    I hope this helps 🙂

  • Hi James @acf-support,

    I was asking more along the lines of modifying the WP Search Query to search through repeater fields. Your solution above doesn’t show any results because the WP search query, by default, only looks at the title and content of a post. I am using SearchWP which allows to index custom fields, but repeater fields each need to be declared separately for indexing (please see attachment). This is pretty counter intuitive, as there is a chance that the repeater increment can be beyond what is declared.

    Also, the results count is still considered as 1 (ONE) according to WP even if the query appears in multiple repeaters in ONE post.

    I found this code (i haven’t tested yet) but it seems to only be for regular custom fields, not repeater fields. Do you think you can modify it to include repeaters?

    https://gist.github.com/charleslouis/5924863

    Thanks.

  • Hi @bsebastian86

    In this case, you need to override the search query. You can check how to query the repeater values here: https://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/.

    Hope this helps 🙂

  • Hi James @acf-support,

    Great! Nearly there. How do I go about including both the Title and Content sub fields in the SQL? The example only shows for one sub field.

    "
    SELECT * 
    FROM {$wpdb->prefix}postmeta
    WHERE meta_key LIKE %s
    AND meta_value = %s
    ",
    'images_%_type', // meta_name: $ParentName_$RowNumber_$ChildName
    'type_3' // meta_value: 'type_3' for example
  • Hi @bsebastian86

    In this case, you need to query the wp_post table too. Unfortunately, it needs an advanced query and more related to WordPress and MySQL. In this case, kindly visit WordPress Comunity instead.

    Here’s a great thread on how to query multiple tables: http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables.

    Hope this makes sense 🙂

  • Thanks for the help James @acf-support! I’ll see if I can take it from here! Cheers!

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

The topic ‘Showing ONLY repeater rows that contain a certain keyword’ is closed to new replies.