1

I am wanting to dispaly custom post types on my archive pages using code from here:

https://wordpress.stackexchange.com/questions/179023/adding-custom-post-types-to-archive-php

but this now gives WP error message of:

Warning: Invalid argument supplied for foreach() in /wp-includes/class-wp-post-type.php on line 613

add_action('pre_get_posts', 'query_post_type');

function query_post_type($query) {
  if($query->is_main_query()
    && ( is_category() || is_tag() )) {
        $query->set( 'post_type', array('post','cpt') );
  }
}

Please let me know what changes are needed to not return errors.

3
  • what are the names of your custom post types? are you trying to include more than one custom post type into the archive?
    – Michael
    Commented Jan 5, 2020 at 5:50
  • No, I just want to show 1 cpt in the archive. The specific name is "asana" and if I change this nothing shows in the archive. Commented Jan 5, 2020 at 12:25
  • You shouldn't need any code. As long as has_archive is true when registering the post type you will get an archive automatically at /asana/ Commented Jan 6, 2020 at 0:57

1 Answer 1

0

The problem could be related to how you register the custom post type.

Did you add the "taxonomies" argument in the register_post_type? If yes, is it a string? Try to convert it in an array of strings.

See the the example below...

    register_post_type(
        'custom_post_type_slug',
        array(
            'labels'               => array ( /* ... */ ),
            'public'               => true,
            /* ... */
            'taxonomies'           => array( 'custom_post_taxonomy_slug' ),
        )
    );  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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