发布帖子时,我需要检查帖子内容是否有分隔符ID名称,以便更改自定义数据库表中的一些值。
问题是publish_post
hook或其他我尝试过的方法总是检查旧帖子的内容,而不是新的更新内容,这意味着我必须发布两次帖子才能得到正确的测试:
add_action( \'publish_post\', \'psidPaginationFunction\', 99, 0 );
function psidPaginationFunction() {
global $post;
global $wpdb;
$table_name = \'wp_psid_custom_navigation\';
setup_postdata( $post->ID );
$content = $post->post_content;
$permalink = get_permalink( $post->ID );
if ( strpos( $content, \'psid-mainPost\' ) !== false ) {
$wpdb->insert($table_name,
array(
\'post_id\' => $post->ID, //replaced non-existing variables $lq_name, and $lq_descrip, with the ones we set to collect the data - $name and $description
\'is_parent\' => \'1\',
\'is_children\' => \'0\',
\'permalink\' => $permalink ,
),
array(
\'%d\',
\'%d\',
\'%d\',
\'%s\'
)
);
}
}
最合适的回答,由SO网友:bouzidi zied 整理而成
我认为追本溯源是解决这个问题的最好办法,This is a timing / synchronization problem as i can see, 因此,当我使用sql查询读取数据库中的更新值时,我终于可以正常工作了,here is what i have added to get the UPDATED content :
$querystr = "SELECT $wpdb->posts.post_content FROM $wpdb->posts WHERE $wpdb->posts.ID = $post->ID";
$content = $wpdb->get_row($querystr, OBJECT);
$content = $content->post_content;