wordpress数据表及火车采集数据库发布

  • wordpress特色图片featured image写入数据库,先发布文章post,然后需要将图片作为post发布,再添加 _thumbnail_id 和 _wp_attached_file ,缺一不可:
INSERT INTO wp_posts (post_title,post_content,post_author,post_date,post_excerpt,to_ping,pinged,post_content_filtered,guid,post_type,post_status,post_mime_type) VALUES ('[标签:标题]','','[标签:authorid]','[系统时间转化:yyyy-MM-dd HH:mm:ss]','','','','','[标签:img-url]','attachment','inherit','image/[标签:image-type]');

INSERT INTO wp_postmeta(post_id,meta_key,meta_value) VALUES ([文章编号:wp_posts]-1,'_thumbnail_id',[文章编号:wp_posts]);

INSERT INTO wp_postmeta(post_id,meta_key,meta_value) VALUES ([文章编号:wp_posts],'_wp_attached_file','[标签:img-url-2]');
  • 同一张数据表如果要插入多组数据,可以:
INSERT INTO

wp_term_relationships (object_id,term_taxonomy_id)

VALUES

 ('[文章编号:wp_posts]','[标签:catid]'),
 ('[文章编号:wp_posts]','[标签:catid2]'),
 ('[文章编号:wp_posts]','[标签:catid3]');

但是火车采集不支持这种SQL写法,会报错,只能每次插入一行,分开写。

  • 如果要获取上一篇post的自增id,即post_id,可以用set变量:
set @var=[文章编号:wp_posts];
INSERT INTO wp_postmeta ( post_id,meta_key,meta_value)
VALUES
(@var-1,'_wp_attached_file','shows/[标签:img]'),
(@var-1,'_movie_poster',@var);

但是,火车采集同样不支持set变量。不过可以直接用减法运算:

INSERT INTO wp_postmeta(post_id,meta_key,meta_value) VALUES ([文章编号:wp_posts]-1,'_movie_poster',[文章编号:wp_posts]);
  • 前面关于wordpress火车采集数据库发布的文章:

有时需要更新wp_posts补全其他字段,否则会报错:

INSERT INTO wp_posts (post_title,post_content,post_author,post_name,post_date,post_excerpt,to_ping,pinged,post_content_filtered) VALUES ('[标签:标题]','[标签:内容]','[标签:authorid]','[标签:slug]','[系统时间转化:yyyy-MM-dd HH:mm:ss]','','','','');
  • 关于wordpress几张数据表的对应关系,前面也有:

用的比较多的时posts和category、tag之间的对应关系,经常容易搞错,我做了个说明图,一目了然。

  • 数据表的自增id是根据上一个(最后一个)id递增的,如果需要重置,或者设定从某id增加,执行下面代码
//设置sysuser_account表从88开始自增

alter table sysuser_account auto_increment=88;

wordpress自动设置文章第一张图为特色图

下面这段代码会将文章中第一张WordPress附件图片设置为特色图像,附件图片是指上传到WordPress的图片,不需要和当前的文章有关联。

function sola_auto_featured_image() {
  global $post;

  if( has_post_thumbnail() ){
    return;
  }

  preg_match('/<img\s[^>]*?class\s*=\s*[\'\"]([^\'\"]*?)[\'\"][^>]*?>/', $post->post_content, $matches);
  $img_class = $matches[1] ?? false;

  if( $img_class ){
    preg_match('/wp-image-([\d]+)/', $img_class, $matchId);
    $attachment_id = $matchId[1] ?? false;

    if( $attachment_id ){
        set_post_thumbnail($post->ID, absint($attachment_id) );
    }
  }
}

add_action('the_post', 'sola_auto_featured_image');
add_action('save_post_post', 'sola_auto_featured_image');

注意:add_action(‘the_post’, ‘sola_auto_featured_image’)会使这段代码在任何文章被加载时运行,比如archive页面,single post页面等等。好处是被人访问一段时间,你的所有文章就自动获得了特色图像,坏处就是对性能有影响。当你的特色图像设置的差不多时,应该将这一句注释掉。

继续阅读

WordPress优化之缓存侧边栏

之前有写关于wordpress优化的文章:

使用下来,Opcache+Memcached+WP Rocket组合效果非常好,但是无意间发现,原来这整个组合,也没有让wordpress侧边栏被缓存,也就是说,wordpress侧边栏是动态查询的,尤其是如果侧边栏有相关推荐,等动态内容的时候,每次打开页面wordpress都会动态查询数据库,即使用了 Opcache+Memcached+WP Rocket ,也不会缓存侧边栏。

update:关闭WP Rocket预缓存,就可以缓存侧边栏,原文连接在文章结尾;

同时,下面的方法对于开启wordpress多站点的,会有问题,可能会出现因跨域加载而页面混乱。

网上找到我爱水煮鱼关于缓存侧边栏的文章,试验下效果不错。但是他的代码有几个语法错误,保存后会报错,网上其他人抄代码的,竟然都没有指出错误,错误代码一起抄!!

我已经修改过来了。

  1. 进入 WordPress 后台,点击外观 => 主题编辑 => Sidebar (sidebar.php)。
  2. 在 sidebar.php 开头加入以下代码:
<?php 
$sidebar_html = ABSPATH . "wp-content/cache/sidebar.txt";
$have_cached = false;
if (file_exists($sidebar_html)){
    $file_time = filemtime($sidebar_html);
    if (($file_time + 3600) > time()){ //缓存1小时
        echo "<!-- cached sidebar -->";
        echo(file_get_contents($sidebar_html));
        echo "<!-- end of cached sidebar -->";
        $have_cached = true;
    }
}
if(!$have_cached){
    ob_start();}
?>

在 sidebar.php 结尾加入以下代码:

<?php
    $sidebar_content = ob_get_contents();
    ob_end_clean();
    $sidebar_fp = fopen($sidebar_html, "w");
 
    if ($sidebar_fp){
         fwrite($sidebar_fp, $sidebar_content);
         fclose($sidebar_fp);
    }
 
    echo $sidebar_content;
?>
继续阅读