Custom Post Type Display Linked to Common Posts

Asked

Viewed 343 times

0

I created a custom post type and in it 4 posts, and called right in the code, I believe, only that they only appear when I leave some post created in post type Posts which is the standard of Wordpress.

<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>
  • Probably the have_posts() function checks for Post type records.

  • How do I pull only the custom post type I created?

1 answer

0


Your problem is here:

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

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

To do this, try mounting your query as follows:

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

$query = new WP_Query( $args );

Thus, $query becomes a query object, not just a post object. To loop, you can ride

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 of the existence of a post common.

  • Thank you very much Caio. Coming home I will test.

  • as I’m still crawling both in WP and PHP, I just haven’t been able to correctly insert my code (Divs) next to this loop ae.

  • It worked Caio, even now excluding the post in the post type Posts, the posts of my CPT no longer disappear. I just have to be able to display right now only the posts referring to the taxonomies that put in the arguments, even putting, ta appearing all that have in the Custom Post Type, and not only those that marked the category...

  • @If the answer helped you, don’t forget to mark it as correct. As for your question about taxonomies, you end up setting up a completely different question. Try looking in the questions here, because I think I’ve answered something of the kind myself. If you don’t find it, open a new question and the community will surely help you.

  • Opa, it was worth too much Caio, the answer helped a lot yes and on taxonomy I already managed to solve tb, Bro Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.