Support

Account

Home Forums Add-ons Repeater Field Querying database for repeat sub fields displaying revisions

Solved

Querying database for repeat sub fields displaying revisions

  • Hi All,

    I have a query which pulls results from a repeater’s sub fields as per the example in the documentation.

    At first it was working fine, but it appears now to be display results multiple times. I believe the multiple iterations are results from revisions of the page. Is there a way of filtering these out so that each result is only displayed once?

    Essentially I have an awards page on the site, which has all the awards within a repeater. Within that repeater are a couple of text fields, and a post object field to select a related project for that award. On the awards page that is used to display the project name, and link it to the project itself (a post under a custom post type). All good.

    Then in reverse, on the single-project page I have the query which checks if the project is selected for any of the awards within the awards repeater, and if so, display the list of awards in an unordered list. Code below.

    $rows = $wpdb->get_results($wpdb->prepare( 
            "
            SELECT * 
            FROM wp_postmeta
            WHERE meta_key LIKE %s
                AND meta_value = %s
            ",
            'awards_%_award-project', // meta_name: $ParentName_$RowNumber_$ChildName
            $post->ID // meta_value
        ));
    
    if( $rows )
    {
    	echo '<ul class="project-awards">';
    	foreach( $rows as $row )
    	{
    		preg_match('_([0-9]+)_', $row->meta_key, $matches);
    		$name_key = 'awards_' . $matches[0] . '_award-name'; // $matches[0] contains the row number
    		$year_key = 'awards_' . $matches[0] . '_award-year';
    
    		$award_name = get_post_meta( $row->post_id, $name_key, true );
    		$award_year = get_post_meta( $row->post_id, $year_key, true );
    		echo '<li><span class="award-name">'. $award_name .'</span>, <span class="award-yeaer">'.$award_year.'</span></li>';
    	}
    	echo '</ul>';
    }

    Thanks in advance.

  • Hi @Nathan

    Within your loop, you can check the post_status of the $row->post_id. You can use this to determin if the post is published.

    Please have a read of the WP function get_post_status

    Thanks
    E

  • Thanks Elliot.

    I ended up wrapping the contents of the foreach with a conditional, and it seems to have done the trick.

    foreach( $rows as $row )
    	{
    	if ( get_post_status($row->post_id) == 'publish' ) :	
    		preg_match('_([0-9]+)_', $row->meta_key, $matches);
    		$name_key = 'awards_' . $matches[0] . '_award-name'; // $matches[0] contains the row number
    		$year_key = 'awards_' . $matches[0] . '_award-year';
    
    		$award_name = get_post_meta( $row->post_id, $name_key, true );
    		$award_year = get_post_meta( $row->post_id, $year_key, true );
    		echo '<li><span class="award-name">'. $award_name .'</span>, <span class="award-yeaer">'.$award_year.'</span></li>';
    	endif;
    	}
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Querying database for repeat sub fields displaying revisions’ is closed to new replies.