The proper way to set up a secondary query, using your example code. Instead of this:
$posts = get_posts('numberposts=1&order=DESC&orderby=post_date&category='.get_cat_ID( 'سیاست' ));
Do this:
// Query args
$custom_cat_args = array(
'posts_per_page' => 1,
'order' => 'DESC',
'orderby' => 'post_date',
'cat' => get_cat_ID( 'سیاست' )
);
// Instantiate query
$custom_cat_query = new WP_Query( $custom_cat_args );
// Instantiate loop
if ( $custom_cat_query->have_posts() ) : while ( $custom_cat_query->have_posts() ) : $custom_cat_query->the_post();
// Normal loop markup here
// Close loop
endwhile; endif;
// Restore $post global
wp_reset_postdata();
Using this method, your custom query will play nicely with your main loop query, and any other secondary queries.
Related Posts:
- Display list of Sub-Categories and the posts they contain, within one main Category
- How would you create a “weekly” archive?
- Inside a loop, how to change CSS class based on category name?
- Category archive is displaying all posts rather than the specific category
- Do not show sub categories in the loop on archive-product.php
- Allow user to select categories that will display in post loop
- Insert HTML content in WP Query at specific point
- filter a loop base on specific category
- Show the number of the post
- How to get multiple loop in category.php, my scripts becomes madness
- 2 loops in archive.php (one for each category)
- Category Archives: Show posts categorized in parent category only
- Decreasing the Memory Consumption of a WordPress Site?
- Rearranging posts based on categories
- WP_Query not looking at child category
- How to get the category of the post and link it to the archive (of the category)
- Display posts links from a category group by year
- Sort category page with custom field
- Get a list of commas separated categories inside a loop
- Category Specific Archive
- How to have Multiple Archives Widgets, one archive widget per category (in a different page)?
- “Sticky” posts for each category (archive.php)
- Check if post belongs to any category
- How do I display the posts from a custom post type on a category.php page?
- How to group the loop posts in a particular by author?
- Exclude category from loop not working
- Display “add to cart” button on every listing in product category page?
- If newest post of category is newest post in general, skip first post of category
- Is it possible to select and edit the way the most recent post from a certain category is displayed on the page?
- WP_Query() returns null when results exist!
- Displaying multiple loops based off of category
- How to check category of of custom post type or not
- Set a them for all subset of category
- Show archives by year from just one category
- Redirect category to url with /category
- Enter a variable in the ‘category_name’ parameter
- Second level subcategory
- how to define category loop last class in the third
- Archive.php filtered by pre-determined category
- How do I call posts with a certain tag?
- Category being displayed without base parmalink
- Exclude category from
- Category archive rewrite rule to include category/subcategory?
- Remove product category placeholder image (Woocomerce)
- WooCommerce – How to show specific category first-rest of products-specific category last
- Category archives with monthly pagination
- How to insert category list into post creation page, and retrieve chosen categories?
- Display single category name in woocommerce loop
- Show Posts from Single Category Not Working
- Pagination on category.php and tag.php not working
- Problem with different query loops (and “main loop”) on category template page!
- How to do paging in the loop?
- How to loop custom post_type from (a) specific category(ies)?
- About title on a page
- Display posts of certain categories to specific user roles
- How do I retrieve the category ID (ugly permalinks) in my sub-navigation menu?
- WordPress showing wrong category name
- what file I need to create to work date archive links?
- multiple values in an array for category__and does not work with WP_Query
- How to add icon over specific thumb
- Displaying a custom field in Category Archives
- Display post thumbnail for specific category outside the loop
- List post only under the category, exclude child category content
- How to detect /category and /tag base pages?
- How to get posts using category slug in ClassiPress?
- Category sticky latest
- List categories of a post hierarchically?
- Redirect Problems with Archive Page and GET variables
- Exclude posts without category from loop
- Archives and Categories in Custom Menu
- Show the parent taxonomy by creating shortcode in woocommerce?
- Custom WordPress theme not displaying posts from category
- How Can I Display the Category Description in a Theme with no Category.php or Archive.php?
- Display Category yearly wise in wp
- How to create a category loop on my blog (ACF Plugin)
- Media Library Categories
- How can I add HTML classes for current taxonomy/term hierarchy into my pages to simplify styles?
- Change single.php template based on parent category
- Category archives
- Loop within category’s posts
- Display one post on category.php: Wrong featured image
- How to add an empty entry to masonry?
- WordPress Numeric Pagination with Query String [duplicate]
- Loops in category description
- date.php shows all posts. how to fix it?
- Several loop in search result
- Printing direct descendants of a category with WP_Query
- Archives and category of authors
- Detecting top parent category fails on home page
- How do I hide posts in a category from all listings but still allow the posts to be viewed?
- How can i hide the authors box from a specific set of categories and post types?
- All post of child category not in top category
- How to show monthly archive posts?
- How can I implement pagination on custom category page?
- how to implement next/prev within category archive?
- How to link ACF relationship field to the new Query block in Full Site Editing
- Order Categories by Character Count
- Elementor : display only one category in loop grid
- Is there a standard “Archive” block that shows ALL posts from a specified category?
- Different number of posts in regular loop and CPT category page