最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - get_page_by_title not working inside fucntions.php

programmeradmin2浏览0评论

so here is the deal I have episodes custom post type this post type needs the id of the series to get some data like image and description etc...

Now I am using a plugin to add posts to the site and the plugin only supports custom fields with name and value. The problem is I don't know the id but I know the series name so here is what I did :

I created a custom field with the name of the series on it and do a get_page_by_title() to get the id of the series with the same title theoretically, that should work and it did when I add it on header.php and access the page then it gets executed

but that is not really practical so I tried adding it to functions.php so it would be executed after each post is published, but when I dod this code inside functions.php it does not work at all

here is the code

    function set_private_categories($post_id) {



    $posttitle = get_field('serie');
    //$mytitle = 'The Fosters saison 4';
    $mytitle2 = get_page_by_title( $posttitle, OBJECT, 'series' );
    //print_r($mytitle2->ID);
    update_field( 'sora_series', $mytitle2->ID, $post_id );
}
add_action( 'save_post_watch', 'set_private_categories' );
发布评论

评论列表(0)

  1. 暂无评论