Exibindo vários tipos de postagem na página inicial

Eu li vários exemplos de como fazer isso, mas ainda não consegui-lo funcionar – cada vez que uso get_posts () parece manter e exibir o primeiro conjunto de resultados.

loop.php

function home_index() { global $post; echo '
    '; $args = array( 'numberposts' => 3, 'offset' => 0, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'gp_news', 'post_status' => 'publish', 'meta_key' => '_thumbnail_id', 'meta_value' => 1, 'meta_compare' => '>=' ); $myposts = get_posts( $args ); foreach( $myposts as $post ) { setup_postdata($post); echo '
  • '; if ( has_post_thumbnail() ) { the_post_thumbnail( 'homepage-thumbnail' ); } ?>

    news <a href="https://wordpress.stackexchange.com//questions/16077/displaying-multiple-post-types-on-home-page/" title="Permalink to " rel="bookmark">

    <a href="https://wordpress.stackexchange.com//questions/16077/displaying-multiple-post-types-on-home-page/">Continue... <?php echo '
  • '; } $args = array( 'numberposts' => 1, 'offset' => 0, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'gp_competitions', 'post_status' => 'publish', 'meta_key' => '_thumbnail_id', 'meta_value' => 1, 'meta_compare' => '>=' ); $myposts = get_posts( $args ); foreach( $myposts as $post ) { setup_postdata($post); echo '
  • '; if ( has_post_thumbnail() ) { the_post_thumbnail( 'homepage-thumbnail' ); } ?>

    competition <a href="https://wordpress.stackexchange.com//questions/16077/displaying-multiple-post-types-on-home-page/" title="Permalink to " rel="bookmark">

    <a href="https://wordpress.stackexchange.com//questions/16077/displaying-multiple-post-types-on-home-page/">Continue... <?php echo '
  • '; } echo '
'; }

Solutions Collecting From Web of "Exibindo vários tipos de postagem na página inicial"

Eu consegui consultar dois tipos de postagem personalizados usando esta linha:

AND wposts.post_type IN ('custom_post_type_1', 'custom_post_type_2')

Isso funcionou para a minha situação, onde cada tipo de postagem personalizado estava sendo ordenado pela mesma meta_key

Então, eu finalmente descobri duas maneiras de fazê-lo. O primeiro caminho é …

 global $wp_query; $args = array( array( 'numberposts' => 3, 'offset' => 0, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'gp_news', 'post_status' => 'publish', 'meta_key' => '_thumbnail_id', 'meta_value' => 1, 'meta_compare' => '>=' ), array( 'numberposts' => 1, 'offset' => 0, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'gp_competitions', 'post_status' => 'publish' ) ); echo '
    '; $temp_query = clone $wp_query; for($index = 0; $index < count($args); $index++) { $myposts = new WP_Query( $args[$index] ); if($myposts->have_posts()) { while($myposts->have_posts()) { $myposts->the_post(); echo '
  • '; if ( has_post_thumbnail() ) { the_post_thumbnail( 'homepage-thumbnail' ); } ?>

    < ?php echo($args[$index]['post_type']); ?>

    < ?php the_title(); ?>

    < ?php the_excerpt(); ?> Continue... < ?php echo '
  • '; } } } $wp_query = clone $temp_query; echo '
';

… mas isso resulta em usar várias consultas.

Outra maneira é …

 global $wpdb; global $post; $querystr = "(SELECT wp_posts.* FROM $wpdb->posts wp_posts, $wpdb->postmeta wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id and wp_posts.post_status = 'publish' and wp_posts.post_type = 'gp_news' and wp_postmeta.meta_key = '_thumbnail_id' and wp_postmeta.meta_value >= 1 ORDER BY wp_posts.post_date DESC LIMIT 3) union (SELECT wp_posts.* FROM $wpdb->posts wp_posts, $wpdb->postmeta wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id and wp_posts.post_status = 'publish' and wp_posts.post_type = 'gp_competitions' and wp_postmeta.meta_key = '_thumbnail_id' and wp_postmeta.meta_value >= 1 ORDER BY wp_posts.post_date DESC LIMIT 1)"; $pageposts = $wpdb->get_results($querystr, OBJECT); if ($pageposts) { foreach ($pageposts as $post) { setup_postdata($post); echo '
  • '; if ( has_post_thumbnail() ) { the_post_thumbnail( 'homepage-thumbnail' ); } ?>

    < ?php the_title(); ?>

    < ?php the_excerpt(); ?> Continue... < ?php echo '
  • '; } }

    Você não deveria ter que fazer tudo isso. A function abaixo permitirá que você controle o que aparece no loop padrão sem ter que reconstruir a própria consulta

     function cpt_pre_get_posts_filter( $query ) { global $wp_query; if ( !is_preview() && !is_admin() && !is_singular() && !is_404() ) { if ($query->is_feed) { /* set your post types in the array below */ } else { $my_post_type = get_query_var( 'post_type' ); if ( empty( $my_post_type ) ) $query->set( 'post_type' , 'my-post-type', 'my-other-post-type' ); } } return $query; } add_filter( 'pre_get_posts' , 'cpt_pre_get_posts_filter' ); 
     $args = aray( 'post_type' => array ( 'post', 'page','event') ); query_posts($args); 

    Fonte