but the issue is that calling is not from a specific category. I mean, it displays posts from the whole category.
Can you better explain, I don’t understand what you mean when you say whole category?
Does the category ‘news’ contain children ( sub categories )
As category_name will select the category and its children, is so use a category id instead – see https://developer.wordpress.org/reference/classes/wp_query/#category-parameters
Firstly, I feel sorry for being unclear. I’ll create 2 separate pages, blog & news. Also, I was planning to create 2 categories for blog & news.
I would like to display selected featured posts that belong to these categories on the home page. Therefore, I was planning to create a “featured” category and after that, I was going to assign the posts into these categories. So I can display them on the home page.
For instance, If I would want to change the featured posts in the future, I would like to be able to update them from the wp panel. I may be assigning one post to 2 categories and so I can display it for one news page and for one featured posts section on the home page.
I hope that makes sense?
Shortly, I would like to display specified posts from news & blog.
Thanks in advance.
OK,
Having a ‘featured’ category and using that will work.
So I’m not sure of the specific issue. Is it you want to select ‘featured’ and ‘blog’ and ‘featured’ and ‘news’
$query = new WP_Query( array( 'category_name' => 'featured+news' ) );
$query = new WP_Query( array( 'category_name' => 'featured+blog' ) );
Alright,
How should I implement the rest of the code after this? Should I keep it the same or sth? And also, I should write these codes to the page template that I’m using right?
Thank you for your precious help.
Your original code started
$args = ( array(
'category_name' => 'news',
'posts_per_page' => 1
) );
// the query
$the_query = new WP_Query( $args ); ?>
Which gives you 1 post in the category ‘news’
If you want all posts in category featured and news use
$args = ( array(
'category_name' => 'featured+news',
'posts_per_page' => -1
) );
// the query
$the_query = new WP_Query( $args ); ?>
( the -1 gives all )
you may want just published post ( rather than draft or scheduled )
$args = ( array(
'category_name' => 'featured+news',
'post_status' => 'publish',
'posts_per_page' => -1
) );
// the query
$the_query = new WP_Query( $args ); ?>
see https://developer.wordpress.org/reference/classes/wp_query/#status-parameters
The documentation is comprehensive https://developer.wordpress.org/reference/classes/wp_query/
OK,
I am so thankful for your time. Have a good one sir!
As I progressed on this topic, I came up with this hedge.
After I specified the number of posts per page like “‘posts_per_page’ => -1”, the posts are not displaying correctly. Other than that, I’ve tried to put the value of “1” the only error is being that all posts in this section are just calling from one content/post.
I’ve shared the screenshot of the error here to explain better.
Thank’s in advance.