Forums

wordpress sql posts query won't display the latest post in a specific category (1 post)

  1. mtsa2
    Member
    Posted 6 months ago #

    Hi I tried to display 5 posts titles from a specific category also show the date, but it won't display the most recent one that just published, I had to change the recent post date 2 months so it'd show up ..

    I used in_category to filter the post does in_category work with sql query ??

    Thanks !

    <ul id="work_items">
    
    <?php
    $max_loop=0;
    $month = array('','January', 'Febuary', 'March', 'April', 'May', 'June', 'July',
    'August', 'September', 'October', 'November', 'December');
    
    $work_items = $wpdb->get_results("SELECT YEAR( post_date ) AS year, MONTH( post_date )
    as month, post_title as title, ID as post_id, COUNT( id ) as post_count FROM $wpdb-
    >posts WHERE post_status = 'publish' and post_type = 'post' GROUP BY year ORDER BY
    post_date DESC");
    
    foreach($work_items as $work_item) {
    
    if(in_category("photography",$work_item->post_id)){
    echo "
    <li>";
    echo "<a href='#'>title).")'>";
    echo $work_item->title;
    echo "<span class='project_date'>".$work_item->year." / ".$month[$work_item-
        >month]."</span>";
    echo "</a>";
    echo "</li>
    ";
    $max_loop++;
    if($max_loop==4){break;}
    }
     }
    ?>

Reply

You must log in to post.

About this Topic

Tags