Display of Custom Post Type linked to Common posts

0

I created a custom post type and in it 4 posts, and I called it right in the code, I believe, only that they only appear when I leave some post created in post type Posts / p>

<div class="all-recipe" >
    <div class="container-fluid">

        <div class="row">

            <?php 
                $args = array(
                    'post_type' => 'posts_blog',
                    'status' => 'publish',
                    'numberposts' => 4,
                    'order' => 'DESC',
                );
                $query = get_posts( $args );
            ?>

            <?php if (have_posts()) : foreach( $query as $post ) : setup_postdata ( $post ); ?>  
                <div class="col-md-3" style="padding: 0;">
                    <div class="recipe-posts-principais" style="background: url(<?php echo get_the_post_thumbnail_url(); ?>)">
                        <button>Click to fade in boxes</button><br><br>
                        <div class="block-infos" style="display: none">
                            <h1><?php the_title(); ?></h1>
                            <h3><?php the_author_posts_link(); ?></h3>
                            <span><?php the_time("d/m/Y"); ?></span>
                            <h1> <?php echo $meta['mensagem'][0]; ?> </h1>
                            <p> <?php echo $meta['descricao-humanidade'][0]; ?> </p>
                            <a href=" <?php the_permalink(); ?>">Saiba mais</a>
                        </div>
                    </div>
                </div> 
            <?php endforeach; endif; ?>
        </div>
    </div>
</div>
    
asked by anonymous 16.04.2017 / 15:33

1 answer

0

Your problem is here:

<?php if (have_posts()) : foreach( $query as $post ) : setup_postdata ( $post ); ?>

What shows that your report is correct: only appear when I leave some post created in post type Posts . If not specified, have_posts() will search the global query object rather than the one you tried to specify just above. I say tried , since you did not explicitly. Although get_posts() makes use of WP_Query internally ( see documentation ), all other methods that this class provides will not be available, and that's exactly what you should do.

To do this, try setting up your query as follows:

$args = array(
    'post_type' => 'posts_blog',
    'status' => 'publish',
    'numberposts' => 4,
    'order' => 'DESC',
);

$query = new WP_Query( $args );

In this way, $query becomes a query object, not just a posts object. For the loop , you can mount

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        # code ...
    }
    // Restaura o objeto de consulta
    wp_reset_postdata();
}

Writing your code this way, you remove the dependency on the existence of a common post .

    
17.04.2017 / 14:43