Support

Account

Home Forums Front-end Issues Trying to compare post title with repeater field

Solved

Trying to compare post title with repeater field

  • I have been trying to accomplish this for months. I have searched the forum and google for many hours and I have been able to get pretty far but I still can not get this to work correctly.

    What I am trying to do is loop through a custom post type called case and compare a field called nurse ( a post object field ), that sits in a repeater, with the current post title. I have been able to do this with other similar scenarios but the difference is that this one is in a repeater field.

    Here is what I have put together:

    
    <?php
    	// filter
    	$post_title = get_the_ID();
    	function my_posts_where( $where ) {
    	$where = str_replace("meta_key = 'shift_details_$", "meta_key LIKE 'shift_details_$", $where);
    	return $where;
    	}
    	add_filter('posts_where', 'my_posts_where');
    	$args = array(
    	'numberposts'=> -1,
    	'post_type'=> 'case',
    	'meta_query' => array(
    			array(
    			'key' => 'shift_details_$_nurse',
    			'value' => get_the_ID(),
    			'compare' => '='
    			),
    		)
    	);
    	$my_query = new WP_Query($args);
    	if( $my_query->have_posts() ) :
    
    	while ($my_query->have_posts()) : $my_query->the_post();
    
    	if( have_rows('shift_details') ): ?>
    
    	<?php while ( have_rows('shift_details') ) : the_row(); 
    	if($post_title == the_sub_field('nurse')):?>
    

    I am sure that this is close but can anyone see what I am missing or doing wrong.

    The above code is supposed to compare a repeater field called nurse from the shift_details repeater with the current post title.

    Please help and thank you all!

  • Gentle Bump. Please help with this one. My client wants to launch ASAP and this is the only thing in the way. This community has helped me alot and I have bought the plugin twice to show support since I can not contribute much code.

    Thank you all for looking!

  • Ok, so I finally figured it out. It turns out I was over complicating it as usual. I will post my solution in case it helps others. I was trying to put together a complicated compare loop where I compare a post object field that sits in a repeater from another post type with the title of the current post and then loop if they matched.

    Anyway, here is what I ended up with:

    <?php
    	$post_id = get_the_title();
    	$wp_query = new WP_Query();
    	$wp_query->query(  array (
    	'post_type' => 'case',
    	)); 
    	if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php while ( have_rows('shift_details') ) : the_row(); ?>
    
    <?php $post_object = get_sub_field('nurse'); if( $post_object ) : $post = $post_object; setup_postdata($post); ?>
    	<?php $nurse_title = get_the_title(); ?>
    	<?php if ($nurse_title == $post_id) { ?>
    <?php wp_reset_postdata(); ?>
    

    So, I am actually just querying the post type and then looping through all repeater rows but then adding a check to see if they match after and continuing the loop if they do. I hope this makes sense and helps someone else out there.

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

The topic ‘Trying to compare post title with repeater field’ is closed to new replies.