dqyz48470 2015-04-08 06:54
浏览 103
已采纳

如何在wordpress中导入插件的自定义字段

I have install plugin Yoast SEO Premium, with this plugin have some field is

yoast_wpseo_metadesc,yoast_wpseo_focuskw

but I can't add althought I used:

add_post_meta($post_id, 'yoast_wpseo_metadesc',$my_post['post_excerpt']);
  • 写回答

1条回答 默认 最新

  • doujie4344 2015-04-08 09:16
    关注

    This problem may be due to several concerns, here are some ideas :

    Prefix meta keys with "_"

    Yoast SEO prefix these custom fields entries with a "_" in the database. The key is not "yoast_wpseo_metadesc" but "_yoast_wpseo_metadesc". Same for "yoast_wpseo_focuskw", it's actually "_yoast_wpseo_focuskw".

    Use update_post_meta() instead of add_post_meta()

    Add_post_meta() can create a custom field for a post if the field does not exist. If it exists, it does not update it. It's always better to use the update_post_meta() function, more flexible. If the custom field already exists, it will be updated. Otherwise, the function will call add_post_meta() to create it. In your case, a field maybe already exists with that name.

    Be careful to where you execute your code

    I think we need more information on where you launch your add_post_meta() function. Depending on the context, the approach is different. Here are some examples:

    In single post page

    If it's on your single page, you can use :

    <?php
    global $post;
    update_post_meta( $post->ID, '_yoast_wpseo_metadesc', $post->post_excerpt );
    update_post_meta( $post->ID, '_yoast_wpseo_focuskw', my_focus_keyword' );
    ?>
    

    In function.php

    Place this code in functions.php, it do that after each post saving, in admin section.

    <?php
    // Launch the update_post_meta on post saving.
    add_action( 'save_post', 'my_yoast_saved_datas' );
    
    function my_yoast_saved_datas( $post_id, $post ) {
    
      // Check that your post is what you want
      if ( $_POST['post_type'] == 'post' ) {
    
        // Check if user can't do that
        if ( ! current_user_can( 'edit_post', $post_id ) )
            return;
      }
    
      update_post_meta( $post_id, '_yoast_wpseo_metadesc',  $post->post_excerpt );
      update_post_meta( $post_id, '_yoast_wpseo_focuskw',  'my_focus_keyword');
    }
    ?>
    

    Loop to update custom fields in one time

    Launch one time, in functions.php or pack it on a plugin to launch on activation.

    <?php
    global $post;
    
    // Array of args, change to your need
    $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'post_status'=> 'publish' );
    
    // Get alls posts
    $my_posts = get_posts( $args );
    
    // Loop on post
    foreach ( $myposts as $post ) {
      setup_postdata( $post );
      global $post;
    
      // Update on create custom fields
      update_post_meta( $post->ID, '_yoast_wpseo_metadesc',  $post->post_excerpt );
      update_post_meta( $post->ID, '_yoast_wpseo_focuskw',  'my_focus_keyword');
    }
    
    wp_reset_postdata();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏