Link to home
Start Free TrialLog in
Avatar of Jon Imms
Jon ImmsFlag for United States of America

asked on

Trunicate Wordpress title in XML file.

Hello,  I'm having an issue with a vendor,  regarding post titles in an XML feed we are sending them.

The majority of the post titles are like this below.
6-Day, Chocolate Dedicated Class A Driving Job – Hannibal, MO

They (vendors) want us to eliminate the city, state from the title.  so would be just
6-Day, Chocolate Dedicated Class A Driving Job

We have over 2500 jobs, with titles like that. So, is it possible to truncate the title in the feed, so it outputs everything up to the -

Below is the template i created, and the actual feed url is here
<?php
/**
* Template Name: Custom RSS Template - sptshafferfeed
*/
$postCount = 500; // The number of posts to show in the feed
$cacheFile = "wp-content/themes/enfold/rsscache/sptshafferfeed.rss";

if(!file_exists(dirname($cacheFile)))
{
  mkdir(dirname($cacheFile));
}

$posts_per_page = 50;
$paged = 0;
$theres_more = true;
$rss_items = array();
if(file_exists($cacheFile) && !isset($_GET["reset"]))
{
  $rss_items = json_decode(file_get_contents($cacheFile),true);
  if($rss_items == null) { $rss_items = array(); }
}

$charset = get_option( 'blog_charset' );
header( 'Content-Type: ' . feed_content_type( 'rss-http' ) . '; charset=' . $charset, true );
?>
<?php echo '<'.'?xml version="1.0" encoding="utf-8"?'.'>'; ?>
<source>
<title><?php bloginfo_rss( 'name' ); ?> - SmartPhoneTrucker - Shaffer Trucking Job Feed</title>
<link><?php bloginfo_rss('url'); ?></link>
<description><?php bloginfo_rss('description'); ?></description>
<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
<language>en</language>

<?php
while($theres_more)
{
  $result = query_posts( array(
    'post_type' => 'job_listing',
    'paged' => $paged,
    'posts_per_page' => $posts_per_page,
    'tax_query' => array(
        array(
            'taxonomy' => 'job_listing_type', //or tag or custom taxonomy
            'field' => 'id',
            'terms' => array('498')
        )
    )
  ) );

  $number_of_results = count($result);


  while(have_posts())
  {
    // Get the next post
    the_post();

    // Check the cache
    if(isset($rss_items[$post->ID]) && (strtotime($rss_items[$post->ID][0]) == strtotime($post->post_modified)))
    {
      // We have a cached item with the same modified date
      echo $rss_items[$post->ID][1];
    }
    else
    {
      // Either the item isn't cached or the last-modified date has changed
      ob_start();
    ?>
	 <job>
	 	<referencenumber><![CDATA[<?php the_ID(); ?>]]></referencenumber>
		<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>
		<url><![CDATA[<?php the_permalink_rss(); ?>?utm_source=ShafferSmartPhoneTruckerXMLFeed]]></url>
		<city>
                <?php 
					// Use an output buffer to put the output of the_job_location() into a variable with the same name
					ob_start(); the_job_location(); $the_job_location = ob_get_clean(); 

					// Now extract the content between the ">" and the "<"
					$pos1 = strrpos($the_job_location, "<"); // Find the "<" of the "</a>" tag
					$pos2 = strrpos($the_job_location, ">", $pos1-strlen($the_job_location)) + 1; // Find the last ">" that occurs before the "</a>" tag
					$the_job_location = substr($the_job_location, $pos2, ($pos1-$pos2));

					// And display the text e.g. "Wheatland, WY, United States"
					//echo $the_job_location;
					echo preg_replace('/([^,]+).*/', '$1', $the_job_location );

				?>
		</city>
		
		<?php
			$mystr=get_the_term_list( $post->ID, 'states');
			$temp=strpos($mystr,">");
			$states=str_replace("</a>","",substr($mystr,$temp+1));
		?>
		<state><![CDATA[<?php echo $states; ?>]]></state>
		<country><![CDATA[US]]></country>	
		<?php
			$mystr=get_the_term_list( $post->ID, 'job_listing_category');
			$myseperator=',';
			$mycount=0;
			$drivertype='';
				while(strstr($mystr,">")){
					$temp_a=strpos($mystr,">")+1;
					$temp_b=strpos($mystr,"<",$temp_a);
					$value_length=$temp_b-$temp_a;
					$value=substr($mystr,$temp_a,$value_length);
					$mystr=substr($mystr,($temp_b+4));//add the number of characters in "</a>" which is 4
				if($mycount>0){$drivertype.=$myseperator;}
					$drivertype.=$value;
					$mycount++;
			}
		?>
		<jobtype><![CDATA[<?php echo $drivertype; ?>]]></jobtype>
		<freight><![CDATA[Reefer]]></freight>
		<description><![CDATA[<?php the_excerpt(); echo do_shortcode( '[about-shaffer]' ); ?>]]></description>
		<requirements><![CDATA[<?php echo do_shortcode( '[Qualifications]' ); ?>]]></requirements>

		
	</job>

				
      <?php

      // Get the item's RSS content and add it to the array
      $rss_item = ob_get_flush();
      $rss_items[$post->ID] = array($post->post_modified, $rss_item);
    }
  }

  // Save our updates after every page
  file_put_contents($cacheFile, json_encode($rss_items));

  // Check to see if there are more pages
  $theres_more = ($number_of_results == $posts_per_page);
  $paged++;
}
  ?>
  </source>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jon Imms

ASKER

Hey thanks Scott,

I did try
		<title><![CDATA[<?php explode(' – ', the_title_rss())[0]; ?>]]></title>

Open in new window


but nothing happened?
Ok, I will test it out on one of one of my test sites and will report back later.
I started to look at this and realized you are probably using https://wpjobmanager.com/document/the-job-submission-form/

You should just be able to use the post_type and post_title https://github.com/Automattic/WP-Job-Manager/blob/master/wp-job-manager-functions.php.
Hey Scott,

Yes I am using WpJobManager.  The problem is we have 2500+ jobs.  

We found that we have multiple jobs, which the only difference is the location. If you searched for a job, you would get a list of jobs with the exact same title,  and you only know the exact location until you go into the single job listing.  By adding city/state in the tile, every title is unique.
I understand.  

I think if you look up the function the_title_rss() it will be made up of several fields. So instead of using that function, you can create a custom function that only uses the two fields you do need and replace that custom function with the_title_rss()