0

I'm facing a problem I can't solve. I'd like to get the excerpt, title and link of the last post containing a particular meta-box button status.

For example: I would like to retrieve the last post with the status "1" on the meta-box button id "hotnews-status". But I don't know how to do this despite my multiple searches on the forum or in Wordpress docs.

The final goal is to display these news in a component generated by the Visual Composer plugin.

Thank you in advance for your help and advice!

2
  • 1
    oh wait, you changed your question to meta-values; which changes the query; I'll edit one sec
    – DevelJoe
    Commented Mar 5, 2021 at 13:39
  • Sorry @DevelJoe, thinking back, I thought it was more convenient to use the value of a meta-box button rather than a tag :) Thank you!
    – Sagaroth
    Commented Mar 5, 2021 at 13:43

1 Answer 1

1

IF HOT NEWS IS A TAG (FIRST QUESTION ASKED FOR TAGS)

You gotta use a WP query where you query posts which have the tag "hot news" and order the result in descending order of the post ID, and then return 1 post, sth like this:

$query_args = array(
  'post_type' => 'your_post_type',
  'tax_query' => array(
    array(
      'taxonomy' => 'your_tag_taxonomy',
      'field' => 'slug',
      'terms' => 'hot_news'
    )
  ),
  'order_by' => 'ID',
  'order' => 'DESC',
  'posts_per_page' => 1
);

// Fire WP post query
$my_query = new WP_Query( $query_args );

// Check if at least one matching post was found
if ( $my_query->have_posts() ) {

  // If yes, iterate through the found post(s)
  while ( $my_query->have_posts() ) {

    // Forward the post iterator to the currently iterated post of the loop.
    // Like this you'll be able to get the iterated posts' content via the
    // ```get_the...``` etc. functions
    $my_query->the_post();

    $posts_title = get_the_title();
    $posts_excerpt = get_the_excerpt();
    $link = get_the_permalink();

    echo $posts_title." ".$posts_excerpt." ".$link;

  }

}

// Reset the global $post object (should always be done after post queries)
wp_reset_postdata();

IF HOT NEWS IS A META KEY (LATER ASKED FOR META)

If you wanna query in function of metavalues, you need to formulate your query args as follows:

$query_args = array(
  'post_type' => 'your_post_type',
  'meta_query' => array(
    array(
      'key' => 'hotnews-status',
      'value' => 1,
      'type' => 'NUMERIC'
    )
  ),
  'order_by' => 'ID',
  'order' => 'DESC',
  'posts_per_page' => 1
);

The logic behind both queries is the following: Each time you create a newpost, that post will get an auto-incremented ID value associated to it (meaning = ID of the most recently added post + 1) in your database. To thus get the most recent post, order your queried posts in function of their post ID in descending order, and return 1 post (posts_per_page parameter of the query). And yeah then you add any additionally needed filters, such as tag / category / taxonomy / metavalue / whatever filters. Clear?

6
  • It's great! You've made so many things clear to me! I'm going to try to implement all this on my side, but I should be able to do it with this information! The point you gave me also about the ID increment will be very useful for future features. Thanks a lot! I'll keep you informed about the implementation of the code.
    – Sagaroth
    Commented Mar 5, 2021 at 13:54
  • 1
    No worries mate; just in case of getting stuck again; check this page of the docs, it's very well documented for a vast amount of different cases of single / combined post queries you may face: developer.wordpress.org/reference/classes/wp_query
    – DevelJoe
    Commented Mar 5, 2021 at 13:55
  • I managed to implement the whole thing, (thanks again), but I would like to know if it is also possible to include the featured image of my article in the request. Is there something similar to get_the_title() but for featured images? Thank you!
    – Sagaroth
    Commented Mar 5, 2021 at 15:25
  • 1
    hehe no worries; I struggled to find that one too before I knew about it; it's get_the_post_thumbnail
    – DevelJoe
    Commented Mar 5, 2021 at 18:08
  • 1
    oh and btw, if you really only need that single post; you may include 'no_found_rows' => true in your query arguments. It is not very well documented; but to say it easily, if you don't need pagination data (which seems to be the case here; like how many posts you retrieved and how many pages that yields etc., so not needed here cause you only query 1), this will result in faster queries when your query has like 6375284765546 potential candidates.
    – DevelJoe
    Commented Mar 5, 2021 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.