Work and Display a Custom Post Type as a normal Post

admin2025-01-07  4

I've created some Custom Posts for my Wordpress page and I can work fine with them. I can create a new one, edit it and display it in "archive-custom_post_type_name.php" and many more things. I can even use the same category as a normal post.

My huge problem is that I can't display them as a normal post in my website. I want to display mixed between normal posts.

I tryed:

add_action( 'pre_get_posts', 'my_get_posts' );

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

But nothing happens. I also tryed:

add_action( 'pre_get_posts', 'add_mi_cpt' );
/**
 * @param WP_Query $query
 * @return WP_Query
 */
function add_mi_cpt( $query ) {    
    if ( $query->is_main_query() )
         $query->set( 'post_type', array( 'post', 'LISTA_CPT' ) );    
    return $query;
}

In my wp-admin I can see my Custom Posts between the normal posts, ut my problem with that snippet is that my webpage then gives me a 404 error.

It's anyway to create Custom Post Types and work with theme as normal posts so I can display all theme mixed in my website?

Thanks!

---EDITED---

I changed my first snippet and now Custom Post Types displays alright in my WP-Admin "Show all posts" page, but not in the website:

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

---EDITED 2.0---

Maybe next step is to change the Loop in page.php to display that custom post types in the loop? My loop looks like this:

<?php
    if ( have_posts() ) :
        // Start the Loop.
        while ( have_posts() ) : the_post();
        ?>

            <?php get_template_part( 'content','page' ); ?>

            <?php
            // If comments are open or we have at least one comment, load up the comment template.
            if ( comments_open() || get_comments_number() ) :
            comments_template();
            endif;
            ?>

        <?php
        endwhile;

    endif;
    ?>

I've created some Custom Posts for my Wordpress page and I can work fine with them. I can create a new one, edit it and display it in "archive-custom_post_type_name.php" and many more things. I can even use the same category as a normal post.

My huge problem is that I can't display them as a normal post in my website. I want to display mixed between normal posts.

I tryed:

add_action( 'pre_get_posts', 'my_get_posts' );

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

But nothing happens. I also tryed:

add_action( 'pre_get_posts', 'add_mi_cpt' );
/**
 * @param WP_Query $query
 * @return WP_Query
 */
function add_mi_cpt( $query ) {    
    if ( $query->is_main_query() )
         $query->set( 'post_type', array( 'post', 'LISTA_CPT' ) );    
    return $query;
}

In my wp-admin I can see my Custom Posts between the normal posts, ut my problem with that snippet is that my webpage then gives me a 404 error.

It's anyway to create Custom Post Types and work with theme as normal posts so I can display all theme mixed in my website?

Thanks!

---EDITED---

I changed my first snippet and now Custom Post Types displays alright in my WP-Admin "Show all posts" page, but not in the website:

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

---EDITED 2.0---

Maybe next step is to change the Loop in page.php to display that custom post types in the loop? My loop looks like this:

<?php
    if ( have_posts() ) :
        // Start the Loop.
        while ( have_posts() ) : the_post();
        ?>

            <?php get_template_part( 'content','page' ); ?>

            <?php
            // If comments are open or we have at least one comment, load up the comment template.
            if ( comments_open() || get_comments_number() ) :
            comments_template();
            endif;
            ?>

        <?php
        endwhile;

    endif;
    ?>
Share Improve this question edited Feb 1, 2017 at 23:19 Roberto Meijide asked Feb 1, 2017 at 17:36 Roberto MeijideRoberto Meijide 116 bronze badges 2
  • Hi there! So you always want to include your custom posts with other, regular posts? Or you just want to do this on, say, your homepage? – nibnut Commented Feb 1, 2017 at 20:13
  • @MacPrawn always with other regular post, not only in the home page. – Roberto Meijide Commented Feb 1, 2017 at 20:40
Add a comment  | 

1 Answer 1

Reset to default 0

Ok, so I think you could apply the codex example as is:

function search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('post_type', array( 'post', 'actualizacion', 'notificacion' ) );
    }
  }
}
add_action('pre_get_posts','search_filter');

I think you were just missing the !is_admin() check in there...

Hope this helps!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736263063a862.html

最新回复(0)