admin管理员组文章数量:1024080
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;
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-
2
Why are you even using
$wpdb
? Your query is perfectly possible withget_posts()
orWP_Query
. – Jacob Peattie Commented Apr 14, 2019 at 5:31 -
Is the value for
$today
correct in both cases? – Jos Commented Apr 14, 2019 at 13:14 - @Jos yes correct in both cases. – mimi Commented 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 Commented Apr 15, 2019 at 6:47
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;
本文标签: widgetsGlobal wpdb is not showing correct data with function call
版权声明:本文标题:widgets - Global $wpdb is not showing correct data with function call 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745588489a2157744.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$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