1

I have a code which shows last 3 recent updated pages or post.

This code is working perfectly when I add it on theme sidebar.php file.

<?php
 $today = current_time('mysql', 1);
 $count = 3;
 if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb- 
 >posts WHERE post_status = 'publish' AND (post_type = 'page' OR post_type = 
 'post') AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC 
 LIMIT $count")):
 ?>
 <h4><?php _e("Recent Updates"); ?></h4>
 <ul>
 <?php
 foreach ($recentposts as $post) {
 if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), 
 $post->ID);
 echo "<li><a href='".get_permalink($post->ID)."'>";
 the_title();
 echo '</a></li>';
  }
  ?>
</ul>
<?php endif; ?>

But when I add this code on theme functions.php file and call it in widget with woody snippet php code. Then it shows only the current page as recently updated page 3 times. Here is the function code:

function iq_recent_update(){
 global $wpdb;
 $today = current_time('mysql', 1);
 $count = 3;
 if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb- 
 >posts WHERE post_status = 'publish' AND (post_type = 'page' OR post_type = 
 'post') AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC 
 LIMIT $count")):
 ?>
 <h4><?php _e("Recent Updates"); ?></h4>
 <ul>
 <?php
 foreach ($recentposts as $post) {
 if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), 
 $post->ID);
 echo "<li><a href='".get_permalink($post->ID)."'>";
 the_title();
 echo '</a></li>';
  }
  ?>
</ul>
<?php endif; 
}

on woody php snippet, I am calling it as

<?php iq_recent_update(); ?>

It shows only the current page name. Why it is working differently and how can I solve it?

Thank you

4
  • 2
    Why are you even using $wpdb? Your query is perfectly possible with get_posts() or WP_Query. Apr 14, 2019 at 5:31
  • Is the value for $today correct in both cases?
    – Jos
    Apr 14, 2019 at 13:14
  • @Jos yes correct in both cases.
    – mimi
    Apr 15, 2019 at 6:00
  • Can you run the SQL statement against the database (e.g. In PHPMyAdmin) and see if it comes up with the same results?
    – Jos
    Apr 15, 2019 at 6:47

1 Answer 1

0

With @Jacob Peattie suggestion, I have recoded it & it is working perfectly fine with functions.

/* Recent updated post & pages */
if ( ! function_exists( 'iq_recent_update' ) ) :
function iq_recent_update() {
$orig_post = $post;
global $post;
$args=array(
        'posts_per_page' => 3,  
        'orderby'          => 'modified',
        'order'            => 'DESC',
        'post_type'        => array('post', 'page'),
         'post_status' => 'publish'
);

$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {

echo '<h3>Recent updates</h3>';
        echo "<ul>";
while( $my_query->have_posts() ) {
$my_query->the_post();?>
<li><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
echo "</ul>";
$post = $orig_post;
wp_reset_query();
}
endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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