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
- Is it possible to use the_post 2 times in one loop
- subtracting the current post form then whole loop, which is generating all CPT titles
- Replace Tag Keyword With Link Within Post Content
- Excluding tag Link from html tags in Post content
- How can I get the last post while on the first post for pagination? As if it were infinite
- How to get value of a selected option from select tag and use it in WP_query to filter posts?
- I really need help… I am looking for a solution about 28hours
- WP_Query that targets all categories
- Counting Posts by Category
- Fill the exclude array() in get_posts()
- show image gallery in archives or category page
- show only one category and filter by tag
- Page with Category Returning 1
- The first post appears twice
- Loop and output 4 rows of posts on home page
- filter posts based on menu_order
- Show 5 posts and than 3 posts offset with pagination
- Custom post Query and WordPress Post Query Clash
- Change archive results based on GET request
- wp_query args with relation
- Ajaxify Post Sort
- WP_Query sort by meta_value_num or date
- How to show category image if no featured image is set?
- Get post id outside loop : Notice: Trying to get property of non-object
- how to call recent post content in a loop ( only the content before read more tag)
- Override wp_link_pages pagebreak with filter
- Get 5 most recent categories
- Retrieve posts inside foundation tabs and tab-content split by 3 per row
- Post being duplicated with foreach loop
- Alternative content between posts no repetition
- Start loop from specific post ID
- post->ID displays the same ID number for every post
- WP_Query extended with AJAX
- Manipulate query to show specific post