Pull content from feed with SimplePie into WordPress


HippySunshine

Senior Member
I am trying to pull content from a job vacancy feed into a WordPress website. I've never done this before so not quite got it right. I've managed to pull in the Job Title and Published date, but I can't seem to pull anything else and It's going a little over my head.
Any info/help would be much appreciated.

This is the feed I am pulling from:

HTML:
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel>
   <title>RSS Feed Title</title>
   <link>https://feedlink.com
   <description>RSS Feed Description</description>
   <item>
       <link>https://feedlink/item.com</link>
       <title>Item Title One</title>
       <a10:updated>2019-09-19T11:53:35Z</a10:updated>
       <a10:content type="text/xml">
           <vacancy>
               <vacancyid>019623</vacancyid>
               <vtitle>Item Title One</vtitle>
               <vacancypublishinfoid>1744392</vacancypublishinfoid>
               <vlocation>Location</vlocation>
               <vsalary>£10 ph</vsalary>
               <vexpirydate>07/10/2019 23:59:00</vexpirydate>
               <vmlocation>Location</vmlocation>
               <vmdiscipline>Vacancy intro</vmdiscipline>
               <vadvert>Main vacancy details</vadvert>
               <vreference>HCGWH464286</vreference>
           </vacancy>
       </a10:content>
   </item>
   <item>
       <link>https://feedlink/item.com</link>
       <title>Item Title Two</title>
       <a10:updated>2019-09-19T11:53:35Z</a10:updated>
       <a10:content type="text/xml">
           <vacancy>
               <vacancyid>019623</vacancyid>
               <vtitle>Item Title Two</vtitle>
               <vacancypublishinfoid>1744392</vacancypublishinfoid>
               <vlocation>Location</vlocation>
               <vsalary>£10 ph</vsalary>
               <vexpirydate>07/10/2019 23:59:00</vexpirydate>
               <vmlocation>Location</vmlocation>
               <vmdiscipline>Vacancy intro</vmdiscipline>
               <vadvert>Main vacancy details</vadvert>
               <vreference>HCGWH464286</vreference>
           </vacancy>
       </a10:content>
   </item>
</channel>
</rss>
And this is the php I have in my WordPress template to pull content:

PHP:
<?php       
    include_once( ABSPATH . WPINC . '/feed.php' );

        // Get a SimplePie feed object from the specified feed source.
        $rss = fetch_feed( 'http://feedurl.com' );
        $maxitems = 0;

        if ( ! is_wp_error( $rss ) ) :
            $maxitems = $rss->get_item_quantity( 3 );
            $rss_items = $rss->get_items( 0, $maxitems );
        endif;
    ?>

    <ul class="list">
        <?php if ( $maxitems == 0 ) : ?>
            <li class="post"><?php _e( 'No current vacancies', 'my-text-domain' ); ?></li>
        <?php else : ?>
            <?php
                foreach ( $rss_items as $item ) :
            ?>
                <li class="post">
                    <div class="content-wrapper">
                        <div class="post-title">
                            <h3 class="label">Job Title:</h3> <span class="input"><?php echo $item->get_title(); ?></span>
                        </div>

                        <div class="post-description">
                            <h3 class="label">Job Description:</h3> <span class="input"><?php echo $item->get_description(); ?></span>
                        </div>

                        <div class="container">
                            <a class="apply-link btn round-btn" href="<?php echo $item->get_permalink(); ?>">Apply</a>
                        </div>
                    </div>
                </li>
            <?php endforeach; ?>
        <?php endif; ?>
    </ul>
 

fisicx

Active Member
You need to print_r($rss_items) to the screen to get the field names.

You can then loop through each one to display the data. All you have at the moment is a list of each job - not the details of each vacancy. The way it's written is each vacancy is a single post. You would need to write a script to build each post and display the data.

You can use this function to create the posts: https://developer.wordpress.org/reference/functions/wp_insert_post/
 

HippySunshine

Senior Member
You need to print_r($rss_items) to the screen to get the field names.

You can then loop through each one to display the data. All you have at the moment is a list of each job - not the details of each vacancy. The way it's written is each vacancy is a single post. You would need to write a script to build each post and display the data.

You can use this function to create the posts: https://developer.wordpress.org/reference/functions/wp_insert_post/

Thanks so much for helping.

I did print_r($rss_items) and it threw out the following:

Code:
[attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) [vReference] => Array ( [0] => Array ( [data] => HCGWH464286 [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) ) ) ) ) ) ) ) ) ) ) ) [1] => Array ( [data] => [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => [child] => Array ( [] => Array ( [link] => Array ( [0] => Array ( [data] => https://honeycombgroup.current-vacancies.com/Jobs/FeedLink/1746189?cid=1645&linkType=1&rsid=0&js=0&s=False&t=Relief+Assistant+Domestic+Abuse+Support+Worker&l=Stoke-on-Trent+%26+Staffordshire+Moorlands [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) [title] => Array ( [0] => Array ( [data] => Relief Assistant Domestic Abuse Support Worker [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) ) [http://www.w3.org/2005/Atom] => Array ( [updated] => Array ( [0] => Array ( [data] => 2019-09-19T11:04:02Z [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) [content] => Array ( [0] => Array ( [data] => [attribs] => Array ( [] => Array ( [type] => text/xml ) ) [xml_base] => [xml_base_explicit] => [xml_lang] => [child] => Array ( [] => Array ( [Vacancy] => Array ( [0] => Array ( [data] => [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => [child] => Array ( [] => Array ( [VacancyID] => Array ( [0] => Array ( [data] => 464726 [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) [vTitle] => Array ( [0] => Array ( [data] => Relief Assistant Domestic Abuse Support Worker [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) [VacancyPublishInfoID] => Array ( [0] => Array ( [data] => 1746189 [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) [vLocation] => Array ( [0] => Array ( [data] => Stoke-on-Trent & Staffordshire Moorlands [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) [vSalary] => Array ( [0] => Array ( [data] => £8.21 per hour [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) [vExpiryDate] => Array ( [0] => Array ( [data] => 30/04/2021 23:59:00 [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) [vmLocation] => Array ( [0] => Array ( [data] => Stoke-On-Trent [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) [vmDiscipline] => Array ( [0] => Array ( [data] => Charity & Voluntary, Case Worker, Project Worker, Support Worker, Advice Worker, Domestic Abuse / Violence, Support Worker, Relief Support [attribs] => Array ( ) [xml_base] => [xml_base_explicit] => [xml_lang] => ) ) [vAdvert] => Array ( [0] => Array ( [data] =>
So if I want to pull the [vLocation], how do I go about this?
 

fisicx

Active Member
Try $item[‘vlocation’][‘data’]

I’m out all day today so can’t test this. What are doing is getting data fields from each vacancy in the array. You just need to get the right field names.
 

HippySunshine

Senior Member
Try $item[‘vlocation’][‘data’]

I’m out all day today so can’t test this. What are doing is getting data fields from each vacancy in the array. You just need to get the right field names.
I want to pull the latest 3 vacancies with the following information:
Job title
Salary
Description
Closing date
Location

$item[‘vlocation’][‘data’] Didn't work and threw an error:
Fatal error: Uncaught Error: Cannot use object of type SimplePie_Item as array in
 

fisicx

Active Member
That's not going to be easy.

You first need to sort by the [data] field in the [updated] sub-array. This will put the vacancies in date order. You then need to loop through the array 3 times to get the field data for the latest three vacancies.

It means lot of faffing about with the $item array to find the actual data.

It looks like it might be something like:

$item[child][‘vlocation’][‘data’]

Difficult to be sure without seeing the website.
 

HippySunshine

Senior Member
That's not going to be easy.

You first need to sort by the [data] field in the [updated] sub-array. This will put the vacancies in date order. You then need to loop through the array 3 times to get the field data for the latest three vacancies.

It means lot of faffing about with the $item array to find the actual data.

It looks like it might be something like:

$item[child][‘vlocation’][‘data’]

Difficult to be sure without seeing the website.
That's gone way over my head :(
 

fisicx

Active Member
Sorry. What you are trying to do isn’t easy unless you are comfortable with the Wordpress loop, hooks, wp_query, arrays and php.

I know exactly how to make this work but it would take me longer to explain how to do it than it would to do the actual work.

If you can send me the simple_pie file I’ll knock up a demo for you.
 
Top