If you are on a category page (that is at URL like https://example.com/cateogry/test/ and want to filter your query by “test” category) then you can modify your code to this
global $wp_query;
$query = new WP_Query(
array(
'meta_key' => 'date_event',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'advert_category',
'field' => 'term_id',
'terms' => $wp_query->get_queried_object_id(),
),
)
)
);
This will of course work only if you will run this query once the WP request is parsed and executed, in other words it will work if you run this code after “wp” action.
If you want to add this code in your theme template files then it should work fine.
Related Posts:
- How to Change Order of Posts in Admin?
- Random sort within an already sorted query
- Different post sort order within different categories
- Setting Custom Sort Order of Posts within a Category
- Can I force a metabox to be in one column?
- How to Group Posts by the First Letter or Number?
- How to Arrange Posts by Size in the WordPress Dashboard?
- blog posts sorting doesnt work while using get_query_var
- How to sort posts by last name (2nd word) on ONE category only?
- Group Posts by First Letter of Title
- Sort wp post title with alphabetically and numerically?
- Exclude posts with specific meta_value while sorting by a separate meta_value
- Possible to create a permalink to sort with meta_key?
- How to choose a sort order (for posts) per category? (ideally when creating a new category)
- Plugin for sortable posts in grid [closed]
- How to display sorted posts in the ‘All Posts’ page
- Sort posts by newest child while keeping hierarchy intact
- Filter Posts by Excluding Categories
- Sorting posts alphabetical that have single digits
- Sorting posts according to the term they belong to
- Sorting posts according to view counts not working
- Server-side sorting of posts
- How to sort posts inside categories
- How to display posts via custom taxonomy terms using checkboxes?
- how can i change WP main archives loop to sort by name or title
- how to get post order by post id wp_query?
- Sort Custom Post Type by Category ID then Post Name
- Post filtering is returning blank page
- Sort by last word in title
- Sort All Posts by Category Name in the Admin Panel?
- How to list post as buch of category, and all of them
- How to sort WP_Post Object array by object field in php?
- Ajaxify Post Sort
- How to sort posts in admin by name
- Sort / Filter Queries
- Sorting Posts by Date – get_blogs_of_user_id()
- Sort Posts Alphabetically by Multiple Categories [duplicate]
- How to get alphabetic listing x other posts, based on first letter post?
- Sort by category and then date?
- custom sort posts in archive/taxanomy page
- Filter/Sort Post Form On Taxonomy page
- Sort Posts Alphabetically Based on Specific Category (Divi)
- How to sort posts alphabetically based on a specific parent category
- display all posts in wordpress admin
- Create an “All Posts” or “Archives” Page with WordPress 3.0?
- Post visibility on the basis of roles
- List only child categories a post is in, of a specific parent category
- Unattaching images from a post
- Is it possible to add a first and latest posts link?
- How to change post status from publish to draft using hook in wordpress?
- wp_list_categories link to first post of category instead of calling taxonomy template
- How only display all post related to category
- How can I add a meta-box to the posts editor containing all items of a custom taxonomy as checkbox?
- Add category to all post by using sql query?
- post re-order on my site
- Only display posts after current date
- Invisible spam post in backend
- Private Posts/Pages & Search
- Update post status from “publish” to “trash” for half of posts
- Can I create a not clickable post?
- What is the standard featured image size?
- Allow multiple contributors to one post
- How to check a post exist when the permalink has post id in it?
- How to attach an image to a post through Java
- Hook in to add new post link wp admin?
- transition_post_status hook doesn’t have any POST data when publish with Gutenberg [closed]
- How do I insert a post with custom post type and relate it to a custom taxonomy?
- Dynamically switch template on click
- add to end of post in the loop with plugin
- $post->post_type not working
- How to get post content from an array of ids?
- Hide custom metabox value from custom fields
- See if a post has a specified tag
- Custom wp-query in wordpress rest api
- Is there a way to order children of post?
- How to retrieve certain number of images from a wordpress post?
- WordPress post filter menu
- Send email to author of post on post submission
- Issue with first top post display order
- Let Posts be stored in another table
- Widget to show posts in the sidebar basing on its IDs
- Display message depending on capability outside loop
- Get post category as a separate string and url
- How to link post titles on page A to its posts on page B?
- ACF flexible content block not showing on live site (works locally)
- How to limit number of posts on AMP homepage
- How publish post from pending status
- Template for displaying CPT / Taxonomy URL issues
- Change WordPress names duplicate titles (url)
- My posts page is missing the page title
- Blog Post slider not working
- WordPress template next_prev
- Remove All in One Seo from Posts for Contributors
- Grab next post title in archive page
- How to store post rating system data post independent?
- get_post_meta property of non object
- First post in loop displays twice
- custom post type single page template not working
- WordPress posts [closed]
- Post UI Tabs plugin double fade effect