I am developing a plugin and I want to show my custom post type (projects
) next to the standard post type posts
. I know I can do this by adding some code to the themes functions.php. But is it possible to achieve this within my plugin? The plugin should be usable from the start without requiring the user to do some extra steps.
I am developing a plugin and I want to show my custom post type (projects
) next to the standard post type posts
. I know I can do this by adding some code to the themes functions.php. But is it possible to achieve this within my plugin? The plugin should be usable from the start without requiring the user to do some extra steps.
- When you say "next to", do you mean in a separate box / column, or mixed in with the posts, or at the end of the posts, or something else? – Rup Commented Mar 26, 2019 at 12:44
- All "projects" should be treated as normal posts. So they should be mixed with normal posts (ordered by date of creation) – styx Commented Mar 26, 2019 at 12:55
- 1 Possible duplicate of How to show posts from multiple post types in a single loop? And display them separately on the same template – Gufran Hasan Commented Mar 26, 2019 at 12:57
2 Answers
Reset to default 2Something like this should achieve your goal. You may need to remove the is_main_query()
check depending on where you want this to show up though.
add_filter('pre_get_posts', 'projects_are_posts');
function projects_are_posts($query) {
if (is_admin() || !is_main_query() ) {
return $query;
}
$types = $query->get('post_type');
if (!is_array($types)) {
$types = array($types);
}
if (in_array('post', $types) && !in_array('projects', $types)) {
array_push($types, 'projects');
$query->set('post_type', $types);
}
}
The only way to do this (show CPTs every time Posts are shown) reliably across themes would be to register a new template with your plugin, and have that take over the homepage completely. This would affect styling and functionality pretty dramatically - you could certainly include the normal header and footer, but then anything in between would just be your custom code and wouldn't match the rest of the theme. It would be similar to what WooCommerce does - they create their own templates. If the end user is PHP savvy they can copy that template into the theme and customize, but most users who just want a plugin to add a custom query aren't necessarily PHP savvy.
Themes are built in such diverse ways - from the PHP code itself to the CSS that can be dependent on a certain number of items - so unless you completely take over a template, you wouldn't have any guarantee of always safely adding the CPT query wherever posts are displayed. However, you could also set up your plugin to have an options page that covers the standard templates, and for example, have options for homepage, single, categories, archives, Pages, etc., and then in your plugin, only add the custom CPT query when it is that type of query and the option is on. So for example, for your homepage question, set up the condition as if(is_home && get_option('home_cpt') == 1)
, and so on.