ReynierPM
ReynierPM

Reputation: 18660

Display post from custom post type

I have this Custom Post Type (CPT) defined at functions.php:

register_post_type(
    'opinion',
    array(
        'label'              => __( 'Opiniones' ),
        'singular_label'     => __( 'Opinión' ),
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'menu_position'      => 2,
        'hierarchical'       => false,
        'rewrite'            => array( 'slug' => 'opinion', 'with_front' => true ),
        'has_archive'        => true,
        'query_var'          => true,
        'supports'           => array( 'title', 'editor', 'excerpt', 'thumbnail', 'author' ),
        'capability_type'    => 'opiniones',
        'capabilities'       => array(
            'publish_posts'       => 'publish_opiniones',
            'edit_posts'          => 'edit_opiniones',
            'edit_others_posts'   => 'edit_others_opiniones',
            'delete_posts'        => 'delete_opiniones',
            'delete_others_posts' => 'delete_others_opiniones',
            'read_private_posts'  => 'read_private_opiniones',
            'edit_post'           => 'edit_opinion',
            'delete_post'         => 'delete_opinion',
            'read_post'           => 'read_opinion',
        )
    )
);

And have this code at front-page.php:

<?php
$prev_post_ids = array();
$prepost       = $post;
$normal_args   = array(
    'ignore_sticky_posts' => 1,
    'order'               => 'desc',
    'meta_query'          => array(
        array(
            'key'     => '_custom_blog_enhome',
            'value'   => '1',
            'compare' => '='
        )
    ),
    'post_status'         => 'publish',
    'posts_per_page'      => 6
);

$normal_query  = new WP_Query( $normal_args );

if ($normal_query->have_posts()) {
    while ($normal_query->have_posts()) {
        $normal_query->the_post();
        $prev_post_ids[] = $post->ID; ?>
        <?php $field = 'custom_blog_exclusivo';
        if ( ! ($$field = get_post_meta( $post->ID, '_'.$field, true ))) {
            $$field = "";
        } ?>

        // here goes the HTML markup for display the post not relevant here

    <?php }
}
$post = $prepost;
wp_reset_postdata(); ?>

But CPT from slug=opinion aren't show. What I need to change in order to display them on front-page.php?

Performing test

I'm doing some test and my code now looks as follow:

$prev_post_ids = array();
$prepost       = $post;
$normal_args   = array(
    'ignore_sticky_posts' => 1,
    'order'               => 'desc',
    'meta_query'          => array(
        array(
            'key'     => '_custom_blog_enhome',
            'value'   => '1',
            'compare' => '='
        )
    ),
    'post_status'         => 'publish',
    'posts_per_page'      => 6,
    'post_type'           => array('opinion','especiales','clasificados','portadadeldia','anunciantes','post','pages')
);

$normal_query  = new WP_Query( $normal_args )

But still not showing posts from opinion, why? Any advice?

Upvotes: 0

Views: 61

Answers (1)

JeremyE
JeremyE

Reputation: 1418

It looks like you need to add post_type => "opinion"to your query args.

$normal_args   = array(
'ignore_sticky_posts' => 1,
'order'               => 'desc',
'meta_query'          => array(
    array(
        'key'     => '_custom_blog_enhome',
        'value'   => '1',
        'compare' => '='
    )
),
 'post_type'             => 'opinion'
'post_status'         => 'publish',
'posts_per_page'      => 6
);

Upvotes: 1

Related Questions