Tags are a taxonomy called post_tag
. You can use them in get_posts()
via the tax_query
.
Since wp_get_post_tags()
returns an array of objects, you need to clean it up a bit since only one field per object is required for the query.
$tag_objects = wp_get_post_tags($post->ID);
$tags = array();
foreach ($tag_objects as $tag_object) {
$tags[] = $tag_object->term_id;
}
$myposts = get_posts(array(
'numberposts' => -1,
'offset' => 0,
'category__in' => array($category),
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => $tags,
),
),
'post_status'=>'publish',
'order'=>'ASC'
));
Related Posts:
- WP Query – duplicated posts once including tags in search results
- Should we trust the post globals?
- Get first post from wp_query
- WP_Query: Why is sticky post not first item in loop?
- Query *only* sticky posts
- How do I reorder (pop and push) items from WP_Query?
- How to exclude latest x posts from a paginated query?
- Reverse chronology of post listing
- WordPress Number of Posts Not Changing With posts_per_page
- This AJAX Code Doesn’t Work – Looking for elegant solution
- Specific loop in Shortcode
- Search widget breaks when using multiple loops?
- Converting multiple loops into one single loop with pagination
- Display posts with comments closed, with pagination?
- Group posts by year in loop
- How would I get 1 latest post from a query for 5 posts?
- WP Query related posts by current page Tag ID
- Loop through all product posts?
- wp_tag_cloud() and the_taxonomies() work but not the_tag()
- Why WP_Query(‘showposts=5’) shows only 1 post?
- Blog post per page setting conflicting with custom WP_Query?
- How to exclude latest x posts from a paginated query?
- WP Query – Posts Per Page not working in combination with category__in
- Pausing and Resuming WP_Query results
- How to : pagination in 3 different custom loops on the same page
- How to add tags (custom taxonomy) to post class css?
- List authors with the last post title and order by last post date
- wp_query random post
- How to get Tags with specific post id
- Possible to alternate between two loops
- WordPress query_posts by tag doesn’t work anymore(?)
- How to get posts published on the latest date?
- Page and post loop same template
- Getting blog pagination to work on page set as front page
- Extracting relevant tags associated with that particular single post only
- Display current post position in Elementor Posts widget
- WP_Query: Show 10 posts in date order, first three random
- WordPress Loop: How to display recent posts in multiple divs
- How to display post list in a table layout (multiple queries in single loop)
- Not all posts showing in query
- how to handle the loop using filling bootstrap grid structure?
- How to check if there are posts with the same tag
- Custom WP_Query not working correctly
- WP_Query of Category Not Showing First Post
- Display 3 posts with different HTML markup using a loop
- While loop in functions.php outputting the_title() of each post on frontpage
- Display specific posts based on an ID of another post
- Numbered Pagination Showing The Same Posts After Altering WP_Query
- Exclude posts in home slider from sections
- show loop id post , only first id can read
- Double loop output
- Loop through all posts showing duplicates
- Can I Paginate Post after every 25 tag?
- Wp_query loop is not working as it should
- Show all posts from the tags without having to set all of them
- WordPress loop uses unmodified posts array, why?
- Advanced Post Display/Pagination/Ordering
- Why I obtain this WP_Query strange behavior trying to select posts having a specific tag?
- How to show the posts list into a static page? Problems to use the loop into a static page
- Adding a category at even positions on main loop with modified pagination
- Make assigning post to a specific category equivalent to assigning it to all categories
- Related posts by searching post tags of single post as terms
- How do I show the post title if an advanced custom field hasn’t been used?
- First post in loop displays twice
- posts_per_page – Repeats only first posts ‘post__in’ array
- Get related posts matching most of the provided tags using WP_Query
- Print Current Post Index number within Loop
- Random sort within an already sorted query
- Search by Hyphen
- rewind_posts() – what actually the use of it, and where using is required or preferred?
- Find most recent authors
- Exclude posts with specific meta_value while sorting by a separate meta_value
- Display the latest posts, published in the last hour, with WP_Query
- Fetch posts from current week (Sunday to Saturday)
- Exclude posts with empty post_content in wp_query
- Exclude posts from featuring
- AJAX filter posts by year
- Should $found_posts be returned as string in ‘found_posts’ filter hook?
- I want to get title name of post, but i didn’t get by this code. Any suggestion?
- Multiple loops are not reset
- Exclude category from
- Display tag image in post using Taxonomy Images plugin index.php
- Sharing post from archive page (loop) doesn’t work
- how can i change WP main archives loop to sort by name or title
- Conditionals if tags exist?
- Change all author links in Blog roll
- Showing categories and subcategories with posts
- why the same code got different results when using query_posts in functions.php and index.php
- WordPress Query Posts From Category Post on Static Page
- Loop through posts? Want to remove extra newlines in actual post HTML/content on backend
- Random posts that always include a particular post?
- Cannot implement byline into posts container in single.php
- Show posts from two specific category in WP_Query
- WordPress get tags in “publish_post” hook
- a WordPress connected to 2 database
- post->ID displays the same ID number for every post
- Post are not showing up
- Display only current page -> posts tags in page
- List posts related to category on a div [closed]
- query_posts different amount of posts per page [duplicate]