Support

Account

Home Forums General Issues Query adding an extra row in table

Solved

Query adding an extra row in table

  • I converted the single custom field example from the following link (https://www.advancedcustomfields.com/resources/query-posts-custom-fields/) to work for my website, and at first it worked fine, but is now not. I have it set up to do the following: find all entries that meet a certain criteria, display each of those entries with other assorted field in a table row. It works, technically, but all of a sudden, it started adding an extra row at the very top, with just one field filled in. You can view this happening over at http://ohana.church/sermon-series/restart/ . What I have happening, is this page calls for a php file that has the code. The weirdest part of this is that the piece at the bottom of this page (http://ohana.church/speakers/Wayne-Surface/) uses an almost identical piece of code, but displays properly. Here is what I have being done for reference. Let me know if you have any other questions, and thanks ahead of time for help!

    <?php 
    
    $name = get_field('series_title');
    
    // args
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'page',
    	'meta_key'		=> 'series_title',
    	'meta_value'	=> $name,
    	'order'			=> 'ASC'
    );
    
    // query
    $the_query = new WP_Query( $args );
    
    ?>
    <?php if( $the_query->have_posts() ): ?>
    <div>
    <table class="SeriesTable cols-8 table-hover">
    	<thead>
        	<tr>
                <th class="SeriesField SeriesFieldCounter"></th>
                <th class="SeriesField SeriesFieldNothing"></th>
                <th class="SeriesField SeriesFieldSeries" scope="col">Series</th>
                <th class="SeriesField SeriesFieldTitle" scope="col">Title</th>
                <th class="SeriesField SeriesFieldSpeaker" scope="col">Speaker</th>
                <th class="SeriesField SeriesFieldDate" scope="col">Date</th>
                <th class="SeriesField SeriesFieldReference" scope="col">Bible Reference</th>
                <th class="SeriesField SeriesFieldSoundcloud" scope="col">Audio</th>
    		</tr>
    	</thead>
    	<tbody>
    	<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<tr>
    			<td class="SeriesField SeriesFieldCounter"></td>
    			<td class="SeriesField SeriesFieldNothing">
                	<a href="http://ohana.church/sermons/<?php the_field('short_title'); ?>/"><i class="fa fa-play-circle"></i></a>
                </td>
            	<td class="SeriesField SeriesFieldSeries">
            		<a href="http://ohana.church/sermon-series/<?php the_field('series_short_title'); ?>/"><?php the_field('series_title'); ?></a>
            	</td>
    			<td class="SeriesField SeriesFieldTitle">
    				<a href="http://ohana.church/sermons/<?php the_field('short_title'); ?>/"><?php the_field('title'); ?></a>
    			</td>
    			<td class="SeriesField SeriesFieldSpeaker">
    				<a href="http://ohana.church/speakers/<?php echo str_replace(' ','-',get_field('speaker_name')); ?>/"><?php the_field('speaker_title'); echo ' ';the_field('speaker_name'); ?></a>
    			</td>
    			<td class="SeriesField SeriesFieldDate">
    				<?php the_field('date'); ?>
    			</td>
                <td class="SeriesField SeriesFieldReference">
                	<a href="https://www.biblegateway.com/passage/?search=<?php the_field('bible_reference_book'); ?>%20<?php the_field('bible_reference_chapter_verse'); ?>&version=KJV" target="_new"><?php the_field('bible_reference_book'); ?> <?php the_field('bible_reference_chapter_verse'); ?></a>
                </td>
                <td class="SeriesField SeriesFieldSoundcloud">
                	<iframe width="100%" height="100" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/<?php the_field('soundcloud'); ?>&show_artwork=false&auto_play=false&show_playcount=false&color=606060&sharing=false"></iframe>
                </td>
    		</tr>
    	<?php endwhile; ?>
    	</tbody>	
    </table>
    </div>
    <?php endif; ?>
    
    <?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>
  • Hi @calebsurface

    It’s possible that you have an empty page with ‘series_title’ field set on that page. You can check it by using this code:

    $the_posts = get_posts($args);
    echo "<pre>";
    var_dump($the_posts);
    echo "</pre>";

    That code will show the list of pages and its ID. Then you can check from the backend if you have that page or not.

    I hope this helps.

  • Well this helped me solve the problem by thinking about it. Basically what was happening is this: I have two separate ACF groups; sermons and series. Every sermon falls into a series, so I had “series_title” as a field name in the sermons field group. In the series field group, I also had “series_title” because, obviously, the series needs a title. I wasn’t thinking about how my code pulled from every group, anything that had “series_title”, so that top row was the entry for the series “Restart”, and thus had none of the other fields filled out because that entry didn’t have those fields at all. Does that make sense? I fixed it by changed the sermons field group to have “series_name” for the field instead of “series_title”.

  • Hi @calebsurface

    Yeah, you need to have unique custom field names for each post, page, taxonomy, etc as ACF heavily depends on it. If you have the same field name, ACF will have a hard time to get the right value for you.

    I hope this makes sense 🙂

  • Yes it does! It’s all working correctly now, so I’m glad I got it worked out. It was one of those problems where I knew that would happen in the back of my head, it just didn’t click. Thanks for your help!

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

You must be logged in to reply to this topic.