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

custom post types - Publish an XML feed from a CPT with ACF fields?

programmeradmin0浏览0评论

looking to develop a website for a recruitment agency and they want to sync their jobs board with Indeed. From chatting to the rep, they said we'll need to provide an XML feed to Indeed in the following format:

.html

Say I was to make a custom post type and have an ACF field for each XML node, how would I go about turning all posts in that post type into an XML field?

looking to develop a website for a recruitment agency and they want to sync their jobs board with Indeed. From chatting to the rep, they said we'll need to provide an XML feed to Indeed in the following format:

https://www.indeed/intl/en/xmlinfo.html

Say I was to make a custom post type and have an ACF field for each XML node, how would I go about turning all posts in that post type into an XML field?

Share Improve this question edited Jan 31, 2020 at 17:28 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked Jan 31, 2020 at 10:39 Marc MurrayMarc Murray 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Here's a fairly complete example of one way to do this.

Let's register a Job post type that will be used with our feed.

/**
 * Register Job post type
 * https://developer.wordpress/reference/functions/register_post_type/
 */
function wpse_register_job_post() {
    $book_args = [
        'label'              => __( 'Jobs', 'textdomain' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => [ 'slug' => 'jobs' ],
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => [ 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ],
    ];
    register_post_type( 'jobby_job', $book_args );
}
add_action( 'init', 'wpse_register_job_post' );

Now for the good stuff. Let's create our custom feed and renderer. The feed can be accessed by visiting http://example/?feed=indeed

Register Feed

/**
 * Create a custom feed.
 * https://developer.wordpress/reference/functions/add_feed/
 */
function wpse_add_indeed_job_feed() {
    add_feed( 'indeed', 'wpse_render_indeed_job_feed' );
}
add_action( 'init', 'wpse_add_indeed_job_feed' );

Render Feed

/**
 * Render our custom feed.
 */
function wpse_render_indeed_job_feed() {
    header( 'Content-Type: ' . feed_content_type( 'rss2' ) . '; charset=' . get_option( 'blog_charset' ), true );

    echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>';
    ?>

<source>
<publisher><?php wp_title_rss(); ?></publisher>
<publisherurl><?php echo get_site_url(); ?></publisherurl>
<lastBuildDate><?php echo date( 'D, j M Y G:i:s' ) . ' GMT'; ?></lastBuildDate>
<?php
    // Get the job posts. Customize arguments as needed.
    $job_query = new WP_Query( [
        'post_type'      => 'jobby_job',
        'posts_per_page' => 42,
    ] );

if ( $job_query->have_posts() ) {
    while ( $job_query->have_posts() ) {
        $job_query->the_post();

        // Note: You'll need to get the various post meta fields and add them below.
        // Use get_post_meta() or for ACF, get_field() can be used.
        // Meta is hard coded in this example.
?>
<job>
    <title><![CDATA[<?php the_title_rss(); ?>]]></title>
    <date><![CDATA[<?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?>]]></date>
    <referencenumber><![CDATA[unique123131]]></referencenumber>
    <url><![CDATA[<?php the_permalink_rss(); ?>]]></url>
    <company><![CDATA[Big ABC Corporation]]></company>
    <city><![CDATA[Phoenix]]></city>
    <state><![CDATA[AZ]]></state>
    <country><![CDATA[US]]></country>
    <postalcode><![CDATA[85003]]></postalcode>
    <description><![CDATA[<?php echo get_the_content_feed( 'rss2' ); ?>]]></description>
    <salary><![CDATA[$50K per year]]></salary>
    <education><![CDATA[Bachelors]]></education>
    <jobtype><![CDATA[fulltime, parttime]]></jobtype>
    <category><![CDATA[Category1, Category2, CategoryN]]></category>
    <experience><![CDATA[5+ years]]></experience>
</job>
<?php } ?>
<?php } ?>
</source>
<?php
wp_reset_postdata();
}

For reference, WP's default RSS feed template is located at wp-includes/feed-rss2.php.

Remember to clear your cache when working on customizing the feed since browsers tend to cache the output.

Some other resources:

  • https://digwp/2012/10/customizing-wordpress-feeds/
  • https://digwp/2009/09/easy-custom-feeds-in-wordpress/
  • Wordpress RSS File Template
  • Cannot get add_feed to work
发布评论

评论列表(0)

  1. 暂无评论