Support » Fixing WordPress » Show all posts of Custom Post Type but NOT in template

  • Resolved Mark Senff

    (@senff)


    So I created a plugin that registers a Custom Post Type named “company”. I did it in a plugin and not in functions.php because I want it to be theme-agnostic.

    What is the best/recommended way of showing all the posts of that post type on the site? I know I can write a query and put that in a template, but then when the theme switches, it wouldn’t show anymore. So that’s not an option, and neither is having a archive-company.php template.

    I tried going to http://www.mysite.com/company but this gives me a 404, I thought this should work but it doesn’t.

    My solution: I created a shortcode in my plugin that queries the CPT and lists them all. So, when the user types [companies] in a post or page, it will list all the posts of that CPT (regardless of which theme is being used).

    It works, but it feels a bit “hacky”. Or is this the way to go?

    My code to register the CPT:

    register_post_type('company', array(
    'label' => 'Companies',
    'description' => '',
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'capability_type' => 'post',
    'map_meta_cap' => true,
    'hierarchical' => false,
    'rewrite' => array('slug' => 'company', 'with_front' => true),
    'query_var' => true,
    'has_archive' => true,
    'supports' => array('title','editor','revisions'),
    'labels' => array (
      'name' => 'Companies',
      'singular_name' => 'Company',
      'menu_name' => 'Companies',
      'add_new' => 'Add Company',
      'add_new_item' => 'Add New Company',
      'edit' => 'Edit',
      'edit_item' => 'Edit Company',
      'new_item' => 'New Company',
      'view' => 'View Company',
      'view_item' => 'View Company',
      'search_items' => 'Search Companies',
      'not_found' => 'No Companies Found',
      'not_found_in_trash' => 'No Companies Found in Trash',
      'parent' => 'Parent Company',
    )
    ) );
Viewing 1 replies (of 1 total)
  • I tried going to http://www.mysite.com/company but this gives me a 404, I thought this should work but it doesn’t.

    Actually, it works after changing permalinks. Still it’s not what I want, because I want to list it in a format/structure that I choose in the plugin (and not anything that’s defined in the loop cause that will differ from theme to theme).

Viewing 1 replies (of 1 total)
  • The topic ‘Show all posts of Custom Post Type but NOT in template’ is closed to new replies.