Use either:
$the_query = new WP_Query('posts_per_page=2&category_name=events');
or
$the_query = new WP_Query(array(
'posts_per_page' => 2,
'category_name' => 'events', // this is the category SLUG
));
// EDIT
Please note that category_name
actually is the category slug (which is for one-word names originally the same). It is not the actual name (maybe having spaces, special characters etc. in it), though. In your case, where you just want to specifiy a certain category, you can do this either by means of the ID (cat=42
) or the slug (category_name=events
).
If you really want/have to use the name, you have to use get_cat_ID('Category Name')
, for instance like so:
$the_query = new WP_Query(array(
'posts_per_page' => 2,
'cat' => get_cat_ID('Events'),
));
Related Posts:
- WP_Comment_Query pagination, delving into the unknown
- Sort posts by category name and title
- Getting all user metadata from user
- ajax category filter
- Display posts the match taxonomy term linked from wp_list_categoies?
- WordPress 4.9.5 PHP intermittent warning trim() expects parameter 1 to be string, array given
- Limit WP_Query to only X results (total, not per page)
- How to disable main query?
- Nested query inside Logical operator OR not working in meta_query
- SQL Statement generated by WP_Query not producing expected results
- Use post__in and post__not_in together?
- WP_Query on different site in a multisite setup
- Best practice for multiple queries on page
- How to order a post type with meta_value_num and if meta_value_num does not exist then order by date
- How to user WP_Query to lower the number of queries
- Exclude post on loop by multiple meta key value
- how do i combine keyword search and taxonomy in a WP_query $args array Worpresss [closed]
- How to find out what “Blog pages show at most” is set to [duplicate]
- difference between $wp_the_query and $wp_query? & getting the values properly [duplicate]
- WP_query category__in not working, only pulls from first category
- tax_query shows no results if nothing is selected
- meta_key and meta_value not working together
- Select random post every day
- How to query for custom field within Gutenberg block from outside of that post? [closed]
- get_query_var returns null
- post thumbnail not showing up with shortcode
- Wp query orderby ‘title’ doesn’t work
- How can I order Wp_Query hierarchically?
- Meta query with JSON value
- Prevent duplicate post from dynamic multiple loops
- Get meta info related to current post
- How to get count of posts assigned to given category?
- Can I count matches without doing a query?
- Wp redirect to url with ampersand string
- Get attachment by meta_key value
- Complex WP_Query order request: DESC by day, but then ASC by time
- How to detect if query is runned from widget?
- Using meta_query with multiple keys and compare values
- Change query based on post type while staying in loop
- Sort wordpress custom posts based on meta value
- Custom Pagination Layout for Elementor widget
- How to properly reset a nested WP_Query query
- How to make search for posts using get method?
- Is there a reason why Pages are not publicly_queryable?
- Exclude some authors from query
- apply custom where to the default $wp_query
- wp_query serialised array
- Conditional operator OR not working with custom fields
- How to add custom meta to ‘pre_get_terms’?
- How do I check if an article is popular this week?
- Using Transients
- Should I reset $wp_query?
- how to write wordpress query for multiple metakeys checking?
- Sorting search results with custom dropdown
- Posts query according to meta box date
- create metabox to activate slider
- querying posts with a custom status
- problem with loop
- WP_Query will not display draft posts
- Randomly select one of last 3 posts
- Query multiple meta key values for template
- How do I get the content of a custom instance of WP_Query?
- Change post query onclick
- Display upcoming events in the next 30 days by querying posts with timestamp in custom meta
- Searching not only by post name but also category
- WP_Query, Paginate by Custom Field
- Query post and sort by custom value calculated with custom field on cpt
- ACF: How to query for a given value count of an array like field? (e.g.: How many rows has a `flexible_content` field?)
- is_user_logged_in not working from WP-API (wp-json)
- WordPress private post won’t display to other admins
- Only show tag with the same id as the post
- Search Exact on WordPress – Relevannsi Solution Not Working
- WP_Query() not working as expected
- How to sort store location by specific category order in WP store locations
- Posts per Page not working
- Trouble with serialized metadata
- Show a message if there are no active posts in category
- Get authors list and sort them by recent posts
- Custom filter from post meta
- Is there documentation for objects returned by WP_Query?
- Pagination not working in custom loop WP_Query
- Multiple wpostmeta.meta_key
- Custom post type loop error: Trying to get property of non-object
- Saving custom fields for WP_Query to retrieve
- Getting the last X posts, but in ascending order of time
- Modify query to exclude certain ids and certain parents and its corresponding children
- How to fix pagination for custom loops on front-page.php
- WP_Query with MetaQuery issue
- Post Query not working after updating to 4.1
- Cannot order by in WP_Query
- How to sort a WP_Query array by post_name after an array_merge();
- Comment count same for every post in homepage WP_Query
- new WP_Query based on average values of meta fields
- WP_Query breaks pagination
- wpdb LIKE request shows all database data
- Can’t seem to get an else statement correct? [closed]
- query_vars category_name only display one catgory out of multiple categories
- Incorrect posts displayed on category page
- Query All Posts: Either Display Most Recent or One with Particular ACF Value Chosen
- Can I use WP_Query to find a post with a particular menu_order?