Display pages and posts in archive - php

I have many categories, but for clearity I will do an example with only two:
News that contains many posts
Products that contains many pages (I've added this feature in functions.php)
I want that archive.php displays:
the posts when the user choose the category News
the pages when the user choose the category Products
At the moment Wordpress by deafult shows only posts and not pages.
I know that this is possible by adding a simple function in functions.php but I can't find it.
EDIT: For another project with custom post type "Books" I used this code
function query_post_type($query) {
$post_types = get_post_types();
if ( is_category() || is_tag()) {
$post_type = get_query_var('books');
if ( $post_type ) {
$post_type = $post_type;
} else {
$post_type = $post_types;
}
$query->set( 'post_type', $post_type );
return $query;
}
}
add_filter('pre_get_posts', 'query_post_type');
Is there something like that for my case?

You can query for solely pages in the WP Query.
PHP:
$args = array(
'post_type' => 'page'
);
$query = new WP_Query( $args );
This will return all pages in your database, and zero posts. Vice versa, you could have 'post_type' => 'post' to query all posts, but that's the default behaviour of the WP Query.
For more documentation on this, check out the WP Query reference in the Wordpress Codex.

Related

Add Custom Templates to Custom Post Type Taxonomies

I created a Custom Post Type, and it has some categories in it.
I created forms with User Frontend Pro for CPT categories.
I’m looking for a solution for assigning the custom templates to that records to show them on the website.
For example;
Assume that I have a CPT named Project.
The project post type has two categories; “Business” and “Ideas”
Users can post their entries with forms to these categories and their posts listed under the account dashboard.
The issue is that;
Business categories should be shown with the category-business.php custom template, and in a similar way, the Ideas category should be shown with category-ideas.php.
How can I add custom template to CPT categories?
P.S. I tried the template hierarchy solution that is in the WP documents, but it's not working. Because there is a custom view function for content. I looking for a code snippet that adds the template page attributes to custom posts automatically, if it is possible.
Assuming that you are just using the default WordPress category you can the pre_get_posts Hook to include the project as part of the category page if they are not showing by default
function include_project_to_cat_pages( $query ) {
if ( $query->is_category() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'project' ) );
}
}
add_action( 'pre_get_posts', 'include_project_to_cat_pages' );
I solved that issue with a filter.
add_filter( 'single_template', function( $template ) {
global $post;
$cat = get_the_category()[0];
if ( $post->post_type === 'project' && $cat->slug === 'business') {
$locate_template = locate_template( "custom-business-template.php" );
if ( ! empty( $locate_template ) ) {
$template = $locate_template;
}
}
return $template;
} );
Thank you to all contributors and the community.

How to add categories for pages only?

I have added this function in my functions.php file to show category option for wordpress pages. It works fine.
function support_category_for_pages() {
// Add category support to pages
register_taxonomy_for_object_type('category', 'page');
}
add_action( 'init', 'support_category_for_pages' );
But is it possible to show/limit some categories for pages only(they must not appear under posts) ?
I hope I am writing this correctly. Basically the requirement is to keep different categories for post & pages and they should not appear in each other's category option.
There are two approaches i can think off. I am not sure how you want it to work regarding the archive page and search page so second approach may not work for you.
First solution creating templates:
For example category-news.php
Inside you can modify the query specificly for this category
$args = array(
'post_type' => 'posts'
//add more arguments if needed
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Do Stuff
} // end while
} // endif
// Reset Post Data
wp_reset_postdata();
Second solution using pre_get_posts:
function wp50_exclude_post_type_pages() {
//in the array you can add ids, slugs or names
if ( is_category( array( 9, 'cat2', 'Category 3' )) && $query->is_main_query() ) {
//here we tell the query to show posts type only
$query->set( 'post_type', 'posts' );
return $query;
}
}
add_action( 'pre_get_posts', 'wp50_exclude_post_type_pages');

Wordpress: have template output all posts with category x where x is assigned dynamically

I am trying to create a series of pages that display the posts within a single category. To do this I use the following PHP code:
<?php
$args = array( 'category' => '$CATEGORY', 'numberposts' => 10000000000);
$myposts = get_posts( $args );
foreach($myposts as $post) :
setup_postdata($post);
?>
My problem is that the $CATEGORY does not seem to contain the category as a string. I have tried using both %s and $id but as I have not declared that it is the category id I am wanting it fails to work. The resulting output has been either an error or all the posts regardless of category.
What argument will convey the category string?
Below is a page illustrating the problem. This is a category page, meaning it should hold all the necessary info. If it was working it would only show the topmost post as it is the only post en site that has the "Press Release" category. Worth mentioning that I have another page just like it called "Dokument" and it displays the press release.
Page: http://www.skinwellness.se/category/pressrelease/
EDIT
I did not notice this before, but it seems that you are using the bundled theme twentythirteen. Simply delete the category.php from your child theme. If you have made changes to the parent theme directly, you should get a fresh copy of the theme, and create a child theme with all your modifications. Never make changes to a theme that you did not write. When such themes update, all you changes will be gone forever
You should then just need the pre_get_posts section in your child theme's functions.php to make everyone work
ORIGINAL ANSWER
You problem is purely your custom loop. As explained in the linked post, you should not be using custom queries in place of the main query on any type of archive page or on your home page
To solve this, revert back to the default loop. This is all you should have. No get_posts or foreach loops
if( have_posts() ) {
while( have_posts() ) {
the_post();
// Add your loop elements here like the_title() and the_content()
}
}
This should fix the problem that when you visit a category page, only the category been viewed will be viewed, no posts from other categories will be shown
Now, if you need to change anything on your category page, use pre_get_posts to do that. Make use of the is_category() conditional tag to target your category pages only. You can also target a specific category with this tag
Say for instance, you need to change the posts per page on you category page, as your case, display all posts with no pagination, you can do this in your functions.php
function so26589648_category_ppp( $query ) {
if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '-1' );
}
}
add_action( 'pre_get_posts', 'so26589648_category_ppp' );
If you need to for example change the order to ASC and need to order posts by author, you can do this
function so26589648_category_ppp( $query ) {
if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '-1' );
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'author' );
}
}
add_action( 'pre_get_posts', 'so26589648_category_ppp' );
You should see WP_Query for all available parameters to use

wordpress the_category for custom post types

I have this loop that gets chosen categories for a custom post type of portfolio:
$wp_query = new WP_Query(array('category__in' => $portfolio_cat, 'post_type' => 'portfolio', 'showposts'=>$number_of_posts) );
It uses standard wordpress categories, but only shows portfolio type posts in those categories. The problem is, I want to list the category the portfolio post is in but when the category is clicked it leads to a 404 not found.
I am using this to display the category (successfully) under the portfolio post type:
<?php the_category('') ?>
Is there any way to have a custom post type use standard categories and not a custom taxonomy and be able to click the category to go to a page that lists all posts in the category?
You have to use
'taxonomies' => array('category')
in your register_post_type() function to make it connected to the category. Also, you can use this
register_taxonomy_for_object_type('category','portfolio');
after you call the register_post_type() function.
If you want your custom post type posts to show up on standard archives or include them on your home page mixed up with other post types, use the pre_get_posts action hook.
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
{
$query->set( 'post_type', array( 'post', 'page', 'portfolio' ) );
}
return $query;
}

Custom Post Type Taxonomies -Posts not showing in Category or Tag pages

I have a made a custom post type called "Member Resources" the posts under this CPT have a few taxonomies such as categories and tags.
Tags = "Diversity" Categories = "Guidance"
When I go to the following urls:
www.domain.com/tags/diversity
www.domain.com/tags/guidance
No posts appear.
Though I have set public => true on the CPT function.
Posts are displaying if you go to the Member Resources archive page though, so they are displaying, but not when you filter them by taxonomies.
Update -
Adding the following code to my functions.php file allows the member-resources CPT to show in Category and Tags pages respectively, but now in the wordpress backend under the "Pages" tab and all other content tabs such as posts etc it seems to have overrided my pages and posts and is showing just the member-resources posts.
add_action( 'pre_get_posts', 'add_my_custom_post_type' );
function add_my_custom_post_type( $query ) {
if ($query->is_main_query())
$query->set( 'post_type', array( 'member-resources' ) );
return $query;
}
your code looks correct. but you are including the CPT member-resources in too many of wordpress's queries. the is_main_query means "the loop" i think.
so you need to restrict this to just running when on a tag archive page.
the following code is from the wordpress site
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
if ( ! is_admin() && is_main_query() && ! $query->get( 'cat' ) )
$query->set( 'cat', '-5' );
}
You need to do a similar thing but determine if you are in a "tags" page.

Categories