Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Bruno Felicio
Bruno Felicio
6,925 Points

Custom fields and WP_query

On the " Build a WordPress Theme" (Displaying Custom Posts and Fields in a Template) when I use add the code that appear on the video, instead of showing the custom posts it shows the normal ones. What am I doing wrong?

7 Answers

Bruno Felicio
Bruno Felicio
6,925 Points

oh man, how could I missed that. Thanks a lot.

Matt Campbell
Matt Campbell
9,767 Points

You need to use an underscore (_) not a hyphen (-) when calling WP arguments.

When writing arguments and functions, basically PHP, WP always uses _ not -. So it's post_type. You've got post-type. Very easy mistake. Save hyphens for CSS classes.

A tip to save time and code for WP_Query is to put your arguments in an array like this:

$query = new WP_Query(array('post_type'=>'adopcion'));
Matt Campbell
Matt Campbell
9,767 Points

What code are you typing? Very hard to help without seeing what it is you're doing.

Bruno Felicio
Bruno Felicio
6,925 Points

I've installed the "advanced custom fields" and "Custom post type UI" as in the tutorial video. Added the post type and custom field.

And in my page template I have:

<?php

/*

    Template Name: adoption

*/

get_header(); ?>

<?php 

    $args = array(
        'post-type' => 'adopcion'
    );

    $the_query = new WP_Query( $args );

?>
<?php if(have_posts() ) : while($the_query->have_posts() ) : $the_query->the_post(); ?>



                <div class="post-featured">

                    <h2><?php the_title(); ?></h2>

                    <p><?php the_field('nombre'); ?></p>

                    <!-- COMMENTS -->




                </div><!-- End of post -->


            <?php endwhile ; else: ?>
                <p>No hay nada.</p>
            <?php endif; ?>

And instead of displaying the custom posts it displays the normal ones and ignores the "<?php the_field('nombre'); ?>"
Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Probably not complete answer solution, but if you add something outside of the loop on this page with HTML, does it show up?