0

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;
    ?>
  • 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 Feb 1 '17 at 20:13
  • @MacPrawn always with other regular post, not only in the home page. – Roberto Meijide Feb 1 '17 at 20:40
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!

| improve this answer | |
  • Yes!! That works great but only works when I search something. What I need is to display them automatically in the posts loop after publish a New Custom Post Type. – Roberto Meijide Feb 1 '17 at 22:44
  • 1
    Then I'd try and remove the if ($query->is_search) { around $query->set... – nibnut Feb 1 '17 at 23:04
  • I just tryed that solution and it's 50% ok. Now I can see my Custom Post Types between WP-Admin "Show all posts" mixed between posts. Perfect!! But now I have to display them as if they were normal posts in my webpage because they don't display yet even with this solution. – Roberto Meijide Feb 1 '17 at 23:10
  • maybe I should change something in the page.php Loop? – Roberto Meijide Feb 1 '17 at 23:19
  • Well, the code I showed you should not affect the admin side of things... You say you see all post types when you click on "Posts" in wp-admin? Could it be you did not negate the is_admin() check, as in !is_admin()? (notice the "!")... That could explain why it works in the back-end, but not in the front-end... You probably do not want the back-end to list your custom posts with the regular ones - that's calling for trouble. – nibnut Feb 2 '17 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.