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
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
Share Improve this question asked Apr 14, 2019 at 5:15 mimimimi 1114 bronze badges 4 |1 Answer
Reset to default 0With @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;
$wpdb
? Your query is perfectly possible withget_posts()
orWP_Query
. – Jacob Peattie Commented Apr 14, 2019 at 5:31$today
correct in both cases? – Jos Commented Apr 14, 2019 at 13:14