• I added categories to my pages and would like both pages and posts tagged with a particular category to show in the default WordPress Latest Posts block.

    I added this code to my functions.php file. It works fine and shows both pages and posts on category pages, but I cannot get pages from a particular category to display in the Latest Posts block. Is there anything I can add to make that work?

    //add categories and tags to pages

    function add_taxonomies_to_pages() {
    register_taxonomy_for_object_type( ‘post_tag’, ‘page’ );
    register_taxonomy_for_object_type( ‘category’, ‘page’ );
    }
    add_action( ‘init’, ‘add_taxonomies_to_pages’ );
    if ( ! is_admin() ) {
    add_action( ‘pre_get_posts’, ‘category_and_tag_archives’ );
    }
    function category_and_tag_archives( $wp_query ) {
    $my_post_array = array(‘post’,’page’);

    if ( $wp_query->get( ‘category_name’ ) || $wp_query->get( ‘cat’ ) )
    $wp_query->set( ‘post_type’, $my_post_array );

    if ( $wp_query->get( ‘tag’ ) )
    $wp_query->set( ‘post_type’, $my_post_array );
    }

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You can still use “pre_get_posts”, but the block query uses “category__in” argument, so your if() conditionals need to check for that as well.

Viewing 1 replies (of 1 total)
  • The topic ‘Latest Posts Block – Display Both Pages and Posts by Category’ is closed to new replies.