修改post ID为增量,忽略posts带自定义字段

Modify post ID to be incremental, ignoring posts with a custom field

我正在尝试将 post id 修改为递增的,同时忽略具有标题为 "foldout" 的自定义字段的 posts。我似乎无法让它工作,而且我没有收到任何 php 错误。我在 functions.php 文件

的末尾有这个
function updateNumbers() {
/* numbering the published posts: preparation: create an array with the ID in sequence of publication date, /
/ save the number in custom field 'incr_number' of post with ID /
/ to show in post (within the loop) use <?php echo get_post_meta($post->ID,'incr_number',true); ?>
/ alchymyth 2010 */
global $wpdb;
$querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.post_status = 'publish' AND wposts.post_type = 'post' AND wpostmeta.meta_key != 'foldout'  ";
$pageposts = $wpdb->get_results($querystr, OBJECT);
$counts = 5 ;
if ($pageposts):
foreach ($pageposts as $post):
setup_postdata($post);
$counts++;
add_post_meta($post->ID, 'incr_number', $counts, true);
update_post_meta($post->ID, 'incr_number', $counts);
endforeach;
endif;
}
add_action ( 'publish_post', 'updateNumbers' );
add_action ( 'deleted_post', 'updateNumbers' );
add_action ( 'edit_post', 'updateNumbers' );

我已将问题缩小到此处提到的查询字符串: https://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query#Query_based_on_Custom_Field_and_Category

我可以让它工作:

$querystr =    "SELECT $wpdb->posts.* 
                FROM $wpdb->posts 
                WHERE $wpdb->posts.post_status = 'publish' 
                AND $wpdb->posts.post_type = 'post' ";

但不是这个:

$querystr =   "SELECT wposts.* 
                FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
                WHERE wposts.ID = wpostmeta.post_id 
                AND wpostmeta.meta_key != 'foldout' 
                AND wposts.post_type = 'post' ";

我根据 "Query based on Custom Field and Sorted by Value" 示例进行了修改。

这似乎可以解决问题,通过更多的谷歌搜索找到了类似的答案。

$querystr = "
    SELECT ID, post_title, post_content FROM $wpdb->posts
    WHERE not exists ( SELECT * FROM $wpdb->postmeta WHERE ($wpdb->postmeta.post_id = $wpdb->posts.ID) AND meta_key = 'foldout')
    AND (post_status = 'publish')
    AND (post_type = 'post')
";