It should be:
$newsposts = new WP_Query('cat=restaurant&posts_per_page=7');
Another way to write it (helps readability with larger queries) would be:
$newsposts = new WP_Query(array(
'cat' => 'restaurant',
'posts_per_page' => 7,
));
See WP_Query
in Codex for description of available parameters.
PS would be good practice to add wp_reset_postdata()
at the end. You are (correctly) not modifying main query, but you do change global $post
variable with this loop.
Related Posts:
- why ignore_sticky_posts in sticky post query
- WP_Query by post ID doesn’t work if post is in custom post_status
- Tracing the life of a query
- Query all posts where meta value is empty
- WP_Query using meta_query with relation OR and orderby meta_value doesn’t work
- Identify which loop you are hooking into; primary or secondary?
- tax_query in WP_Query problem
- Display posts from Last 24 hours and order them based on most views
- Trying to list out child pages with WP_Query
- Pagination is not working using WP_Query
- view queries made?
- Filter and list posts of a custom taxonomy
- Get rewrite result
- WP_Tax_Query with post_tag not working
- How to show recent and upcoming birthdays using meta_query
- How to change posts order on Category and Tag archives?
- How can I retrieve comments from last 5 minutes for a post?
- How WordPress attaches its Featured Images with posts?
- Create a sitemap without a plugin – get parent pages only?
- WP_Query on custom field and order results
- Have functions work outside the loop with 404 override
- WordPress: Display posts by custom select query
- Get ONLY the number of results in a query
- Include custom field in Ajax search WordPress
- Inserting serialized value into wp_postmeta using update_post_meta
- Display custom field meta outside loop, site wide
- Where is the WP_Query instance instantiated (when working with themes)?
- Iterating through $wpdb query without using get_results for large query results
- Extend product search with meta in WooCommerce
- Custom wp_query for related posts with cmb2
- Loop Performance > JSON files vs. WP_Query vs. REST API
- Include data from custom table in WP_Query
- Query posts by last comments
- WordPress ignoring LIMIT / posts_per_page despite being in wp_query
- Conflict with get_posts and the_content
- WP_query exclude posts of a format
- Custom query with meta_key not working fine
- Can I simplify in function column_default use of queries
- How to provide meta_key array to wp_query?
- wp query error while paging the posts
- how to get wordpress user id when user register
- Highlight post in sidebar on single page
- Specify strict ‘order by’ in WordPress query
- How to set up hierarchical relationships without using plugins / meta query
- Single field to search for matches on post ID or meta values
- Getting rid of unwanted nonSQL syntax characters when debugging a query
- array of meta values using WP_User_Query
- Meta query compare for ID’s greater than specific ID
- Using post_where filter only on main query NOT sidebard new WP_Query
- How to avoid bypass of search bar rules?
- Slider won’t work with custom query
- wordpress udpate query not equal to
- How to generate feed for custom template
- WP_Query with one category in args shows other categories
- WP Query – filtering terms with regex
- Custom navigation : show posts in sets of 4
- WordPress WP_Query compare not working?
- Why does apply_filters behave different inside and outside a loop?
- Excluded category from loop
- Add content depending on page loaded
- Custom template for password protected page
- Having a variable based on $post_count increase when i move to the next page of results
- Post order by custom fields
- WP Query and multiple pages
- Elementor posts custom query based on tag [closed]
- How can I do a orderby by the number of items? So basically list by starting with the array with the largest number of post
- How to get all user posts from two post types in wp_query
- How do you do a meta query on an encrypted field?
- meta_query with meta values as serialize arrays
- pre_get_posts pagination not working
- Multiple WP_Query args combinations according to post type
- wp_set_object_terms() custom taxonomy not working correctly when using insert post
- in_the_loop() issue with custom query
- Pagination inside the blog page not working
- How to restrict add media library only to images of the same post family?
- Is my code correct?
- Wp Query sort order
- get_posts query caching?
- wp_query returns match with no criteria
- Adding additional taxonomies to wordpress taxonomy page
- why doesn’t this pre_get_posts code work?
- pre_get_posts with WP_Query to prevent posts from specific tags
- How can I modify standard search query to include also ACF custom fields values?
- Best way to Order Post in Home without a plugin
- Retrieving category pages from subcategory returns empty sets
- Loop through multiple custom fields with increasing number
- Different number of posts showing in development vs production server
- Order WP_Query results in order other than ASC or DESC
- More efficient query to display posts in same sub-category?
- Get post closest to today
- Get WP_Query query after execution?
- Pagination in custom query not working [duplicate]
- $query_args for loop to only get posts from “current category”?
- 294 Queries on Mainpage of WordPress
- How to write a query-function as a query-shortcode?
- taxonomy-{term}.php terms pagination returning 404 after a certain page
- Using WP Query, I want to include all posts in category 1 as long as they are not also in category 2
- Prevent URL Parameter Affecting other WP Query
- Sort by multiple columns using get_posts
- WP_Query filter Posts by timestamp event (range start and end) and by month (next 12 month)