Support » Fixing WordPress » Adding custom posts to archive pages

  • Hi,

    Googling I found the following code:

    function add_custom_types_archive( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'customtype1','customtype2'
                ));
          return $query;
        }
    }
    add_filter( 'pre_get_posts', 'add_custom_types_archive' );

    This worked.
    However, I found it royally screwed up queries that queried post_type and category. Those queries started showing posts categorized appropriately, but only post types NOT the one specified.

    You could see this in the wp backend also. When you clicked on Categories link for a custom type, then clicked the number of posts in for a cat, it showed posts in other types, not the actual custom post type that is displayed at the top of the page.

    I know this is arcane, but any help understanding this, or alternate methods would be appreciated.

Viewing 1 replies (of 1 total)
  • First, is pre_get_posts and action hook instead of a filter?

    I don’t know if this is possible for you, but I use a global variable to switch filters on and off. In the function I check the global and only apply the function if it is set.

    Then, just before I issue the statement where I want the filter, I set the global variable. After the statement, I unset the global variable to turn the filter off.

Viewing 1 replies (of 1 total)
  • The topic ‘Adding custom posts to archive pages’ is closed to new replies.