$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Custom post type show on both Home and Date Archive|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom post type show on both Home and Date Archive

matteradmin14PV0评论

Found below snippet to pull posts from a custom post type 'pictures' into the home page and date archive page..

Added to functions.php this works for home page:

function add_custom_post_type_to_query( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array('post', 'pictures') );
    }
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

But this works for neither:

function add_custom_post_type_to_query( $query ) {
    if ( $query->is_home() && $query->is_date() && $query->is_main_query() ) {
        $query->set( 'post_type', array('post', 'pictures') );
    }
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

Would also like to add in !is_admin() to keep the admin screens clear, and not sure how to go about that.

thanks,

Found below snippet to pull posts from a custom post type 'pictures' into the home page and date archive page..

Added to functions.php this works for home page:

function add_custom_post_type_to_query( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array('post', 'pictures') );
    }
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

But this works for neither:

function add_custom_post_type_to_query( $query ) {
    if ( $query->is_home() && $query->is_date() && $query->is_main_query() ) {
        $query->set( 'post_type', array('post', 'pictures') );
    }
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

Would also like to add in !is_admin() to keep the admin screens clear, and not sure how to go about that.

thanks,

Share Improve this question edited Mar 23, 2019 at 16:33 Qaisar Feroz 2,1471 gold badge9 silver badges20 bronze badges asked Mar 23, 2019 at 4:23 MatJonMatJon 174 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

In your code ( $query->is_home() && $query->is_date() && $query->is_main_query() ) always returns false because $query->is_home() and $query->is_date() cannot be true on same page.
Try this,

function add_custom_post_type_to_query( $query ) {
    if ( ! is_admin()  &&  $query->is_main_query() ) {
       if ( $query->is_date() ||  $query->is_home()  ) {

           $query->set( 'post_type', array('post', 'pictures') );

       }
    }
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

I hope this helps!

Post a comment

comment list (0)

  1. No comments so far