简体   繁体   中英

Include custom post type in Wordpress loop

Sorry, I think I read every post about this but cant get it to work.

I have a Wordpress custom post typ called "external" and I would like to show both the post from "external" aswell as my normal standard posts at the same time on my homepage.

This is my loop:

<?php 
get_header(); 

if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }?>


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
    <?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>    

How can I include the "external" posts in this code?

The theme then uses blocks to display content on the startpage, here is block8.php with a post query.

function block_eight_ajax_query($atts='') {
    $args = array (
$is_ajax = 0;
if($atts==''){
    $is_ajax=1;
    $atts=$_GET;
    if($atts['global_query']){
        unset($atts['no_found_rows']);
        unset($atts['suppress_filters']);
        unset($atts['cache_results']);
        unset($atts['update_post_term_cache']);
        unset($atts['update_post_meta_cache']);
        unset($atts['nopaging']);
    }
}
$atts['is_ajax'] = $is_ajax;
$query = null;
$query = new WP_Query( $atts );
$html = '';
if ( $query->have_posts() ) {
    ob_start();
    $i = 1;
    while ( $query->have_posts() ):
        $query->the_post();

        $atts['video'] = rd_field( 'abomb_post_video' );
        $atts['extern'] = rd_field( 'extern' );
        $atts['audio'] = rd_field( 'abomb_post_audio' );
        $atts['gallery'] = rd_field( 'abomb_post_gallery' );
        $atts['counter'] = $i;
        block_grid_content($atts);
        if ($atts['column'] == 'grid-12') { $divide = 1; }
        else if ($atts['column'] == 'grid-3') { $divide = 4; }
        else if ($atts['column'] == 'grid-4') { $divide = 3; }
        else { $divide = 2; }
        if ($i%$divide==0 && $divide!=1) {
            echo '<div class="clear"></div>';
        }
        $i++;
    endwhile;
    if ($atts['nav'] == 'numbered') {
        echo '<div class="grid-12">';
            rd_pagination($query->max_num_pages);
        echo '</div>';
    }
    wp_reset_postdata();
    $html = ob_get_clean();
    if($is_ajax==1){
        echo $html;
        exit();
    }
    return $html;
}
else{
    if($is_ajax==1){
        echo '-11';
        exit();
    }
    return '';
}
}

Update: I added this code to functions:

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );


function add_my_post_types_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() )
    $query->set( 'post_type', array( 'post', 'extern' ) );
return $query;
}

Now I can see the posts in for exampel the search result but I need the Query in Block8.php to fetch them aswell.

I Thanks!

To include a custom post type in the regular loop (ie. posts page) just add the following code to index.php before if ( have_posts() ) :

$args = array(
'post_type'   => array('post', 'custom_post_type'),
'post_status' => 'publish',
);
$new_post_loop = new WP_Query( $args );

then modify the following two lines:

if ( have_posts() ) : change it to if ( $new_post_loop -> have_posts() ) :

and

while ( have_posts() ) : the_post(); to while ( $new_post_loop -> have_posts() ) : $new_post_loop -> the_post();

This solution avoids the problem of having the custom post type listed in the all posts screen on the backend, which @David.J 's answer produces ;)

Add this to your functions.php file.

/**
* @param WP_Query $query
* @return WP_Query
*/

function add_my_custom_post_type( $query ) {
    if ($query->is_main_query()) 
        $query->set( 'post_type', array( 'post', 'external' ) );
    return $query;
}
add_action( 'pre_get_posts', 'add_my_custom_post_type' );

Ref & duplicate: https://wordpress.stackexchange.com/questions/27104/how-to-display-regular-posts-custom-post-types-that-fall-under-a-category-usin

function add_my_custom_post_type( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if ($query->is_main_query()) {
            $query->set( 'post_type', array( 'post', 'some_custom_post_type_name' ) );
        }
    return $query;
    }
}
add_action( 'pre_get_posts', 'add_my_custom_post_type' );

This is the correct way to write the code so as not to break the dashboard queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM