Adding Query to Custom Page Template

Probably a simple, stupid question, but:

I’m trying to create a page that displays a list of all pages that include a specific ACF value.

I copied page.php from my active theme folder, renamed it page-[list page slug].php, and added the ACF query thusly:

<?php

/**

 * The template for displaying all pages.

 *

 * This is the template that displays all pages by default.

 * Please note that this is the WordPress construct of pages

 * and that other 'pages' on your WordPress site will use a

 * different template.

 *

 * @package SKT Spiritedlite

 */



get_header(); ?>



    <div class="container">

      <div class="page_content">

             <section class="site-main">               

                    <?php if( have_posts() ) :

                            while( have_posts() ) : the_post(); ?>

                                <h1 class="entry-title"><?php the_title(); ?></h1>

                                <div class="entry-content">

                                            <?php the_content(); ?>

<?php 

$posts = get_posts(array(
    'post_type'     => 'page',
    'meta_key'      => 'genre',
    'meta_value'    => 'rock'
));

if( $posts ): ?>

    <ul>

    <?php foreach( $posts as $post ): 

        setup_postdata( $post );

        ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>

    <?php endforeach; ?>

    </ul>

    <?php wp_reset_postdata(); ?>

<?php endif; ?>
                                </div><!-- entry-content -->

                            <?php endwhile; else : endif; ?>

            </section><!-- section-->

    <div class="clear"></div>

    </div><!-- .page_content --> 

 </div><!-- .container --> 

<?php get_footer(); ?>

Uploading this file to my templates folder instantly 500’s my site.

I’m sure I’m missing something obvious, but I’m new to tweaking php at this level.

Thanks!

Solutions Collecting From Web of "Adding Query to Custom Page Template"

On some servers, errors in PHP code cause 500 errors. I can’t test your code right now, but since you said the error only happens when you use your new page template this is most likely.

Yes, it’s really annoying to just have a blank page and not even have a clue of what went wrong. To find the error, you can either:

  1. Make sure your PHP is configured to show errors properly

  2. Look at your PHP error log

Good luck!