最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - Get single post from tags array

programmeradmin0浏览0评论

I have wordpress and many posts. Every post have on tag. Posts may have same tags.

I have:

post1 tag1 date1
post2 tag1 date2
post3 tag2 date3
post4 tag3 date4
post5 tag2 date5

I have get:

post1
post3
post4

That is, I want to select from all posts one post from posts with the same tag and the earliest by date of publication.

I assume the following algorithm:

  1. Get all tags of blog

  2. Get posts for each tag

  3. Get last date post for each tag

  4. Show all posts from step 3.

I have wordpress and many posts. Every post have on tag. Posts may have same tags.

I have:

post1 tag1 date1
post2 tag1 date2
post3 tag2 date3
post4 tag3 date4
post5 tag2 date5

I have get:

post1
post3
post4

That is, I want to select from all posts one post from posts with the same tag and the earliest by date of publication.

I assume the following algorithm:

  1. Get all tags of blog

  2. Get posts for each tag

  3. Get last date post for each tag

  4. Show all posts from step 3.

Share Improve this question edited May 5, 2020 at 13:32 askurashev asked May 5, 2020 at 13:14 askurashevaskurashev 133 bronze badges 2
  • Not sure I understand exactly what you are trying to achieve but I just got done doing something somewhat similar, I think your answer is probably along the same lines. WP Query for Posts (Products) in Specific Category that has 2 Specific Tags (AND both tags not OR) – bbruman Commented May 5, 2020 at 13:21
  • What helped me was looking at the different options in the WP Query Documentation here developer.wordpress/reference/classes/wp_query ... you can filter and get whatever posts you want using this – bbruman Commented May 5, 2020 at 13:22
Add a comment  | 

1 Answer 1

Reset to default 0

$args = array( 'type' => 'post', 'orderby' => 'name' ); $tags = get_tags($args); foreach($tags as $tag) { $the_query = new WP_Query( 'tag='.$tag->name ); if ( $the_query->have_posts() ) { $the_query->the_post(); $desired_posts[] = get_the_ID(); // all the post IDs stored here. } else { // no posts found } wp_reset_postdata(); } $args1 = array( 'post_type' => 'post', 'orderby' => 'date', 'post__in' => $desired_posts, 'posts_per_page' => -1 ); $the_query = new WP_Query( $args1 ); echo '<ul>'; if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>'. get_the_title() . '</li>'; } } else { // no posts found } echo '</ul>';

发布评论

评论列表(0)

  1. 暂无评论