1

I have filtered Urls that I would like to add to push into a sitemap. I am using one of the sitemap plugins, which has hooks to further modify it.

my code:

// add to theme's functions.php
add_filter('bwp_gxs_external_pages', 'bwp_gxs_external_pages');
function bwp_gxs_external_pages($pages)
{
    return array(
        array('location' => home_url('www.example.com/used-cars/location/new-york/model/bmw'), 'lastmod' => '27/03/2017', 'frequency' => 'auto', 'priority' => '1.0'),
        array('location' => home_url('www.example.com/used-cars/location/los-angeles/model/aston-martin'), 'lastmod' => '27/03/2017', 'frequency' => 'auto', 'priority' => '0.8')
                array('location' => home_url('www.example.com/used-cars/model/mercedes-benz'), 'lastmod' => '27/03/2017', 'frequency' => 'auto', 'priority' => '0.8')
    );
}

So as you can see in my code that I have these kind of URLs www.example.com/used-cars/location/new-york/model/bmw & www.example.com/used-cars/model/mercedes-benz

So my issue is that, there are thousands of these URLs and I to push them all into this sitemap.

So my question is that, isn't there a way to perhaps loop them over? than to insert them into the code one by one like so

array('location' => home_url('www.example.com/used-cars/model/aston-martin'), 'lastmod' => '27/03/2017', 'frequency' => 'auto', 'priority' => '0.8')
  • Where is the entire list of links? Are these coming from your Wordpress database? – brianjohnhanna Mar 28 '17 at 20:31
  • I'm not quite sure if there is an entire list of these links. But these links are search queries of taxonomies in the custom post type – joe city Mar 28 '17 at 21:27
  • I see. It looks like this plugin already adds custom taxonomies to the sitemap, you may need to check the documentation and settings. What do you mean by "search queries"? – brianjohnhanna Mar 28 '17 at 21:31
  • You're right, the plugin does add taxonomies. but these urls have about two custom taxonomies attached to them. e.g www.example.com/location/new-york <--- these are custom taxonomies that the plugin adds into a sitemap, www.example.com/used-cars/location/new-york/model/aston-martin <--- these are search queries from the custom post type. used-cars being the custom post type. – joe city Mar 28 '17 at 21:51
1

Here's a stab at it. I'm not sure exactly what in your URL's are taxonomies, or even what your taxonomies are, but basically you'd just have to loop through everything adding to the pages array with the arrays. You'd need to either define all your models in an array, or grab them from the database using something like get_option();

add_filter( 'bwp_gxs_external_pages', 'bwp_gxs_external_pages', 10, 1 );
function bwp_gxs_external_pages($pages)
{
    $models = array( 'aston-martin', 'bmw', 'mercedes-benz' /*...*/ );
    $locations = get_terms( array(
        'taxonomy' => 'location',
        'hide_empty' => true,
    ) );

    // Loop through the search terms
    foreach ( $models as $model ) {
        foreach ( $locations as $location ) {
            $pages[] = array(
                'location' => home_url( '/used-cars/location/' . $location->slug . '/model/' . $model ),
                'lastmod' => '27/03/2017',
                'frequency' => 'auto', 
                'priority' => '0.8'
            );
        }  
        $pages[] = array(
            'location' => home_url( '/used-cars/model/' . $model ),
            'lastmod' => '27/03/2017',
            'frequency' => 'auto', 
            'priority' => '0.8'
        );   
    }

   return $pages;
}

Hope this gets you started.

  • Ah! That got the job done, However when I had the hide_empty on false, I got Fatal error: Maximum execution time of 30 seconds exceeded on line 668 Call Stack Do you have any idea what could be causing that? – joe city May 3 '17 at 19:47
  • @joecity glad it worked for you, I realized that the hide_empty param really should have been set to true, otherwise you're looping through an empty array. Not sure exactly why that would be exceeding your execution time, but obv hard to debug from here – brianjohnhanna May 3 '17 at 20:59
  • Alright Thanx! I guess I'll have to set it to true. thanks alot – joe city May 3 '17 at 21:27
  • Alright Thanx! I guess I'll have to set it to true. thanks alot – joe city May 3 '17 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.