• Resolved oneoff

    (@oneoff)


    hello, friends, I want to add a custom post type in sitemap
    but I didn’t find the rank math functions for doing that

    here my code is working on Yoast SEO
    I will include a few lines of code because that’s what I need to change
    or you need to take a look on the whole code you can get it from here

    https://pastebin.com/uAACDy20

    
    public function get_index_links( $max_entries ){
    		$index = array();
    
    
    	$index[] = array(
    						'loc'     => WPSEO_Sitemaps_Router::get_base_url( 'wp-manga-chapters-sitemap'. $current_page .'.xml' ),
    						'lastmod' => $date,
    					);
    public function get_sitemap_links( $type, $max_entries, $current_page ){
    		$urls = array();
    		

    thanks in advance

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Rank Math SEO

    (@rankmath)

    Hello @oneoff

    Thank you for contacting the support.

    Please use this code:

    <?php
    use RankMath\Sitemap\Providers\Provider;
    
    class Madara_Sitemap_Provider implements Provider {
        /**
         * Check if provider supports given item type.
         *
         * @param string $type Type string to check for.
         *
         * @return boolean
         */
        public function handles_type( $type ) {
            return true;
        }
    
        /**
         * Get set of sitemaps index link data.
         *
         * @param int $max_entries Entries per sitemap.
         *
         * @return array
         */
        public function get_index_links( $max_entries ){
            $index = array();
    
            global $wpdb;
    
            $sql = "SELECT count(*) as total FROM {$wpdb->prefix}manga_chapters";
    
            $count = $wpdb->get_var($sql);
            $total_page = floor($count / $max_entries) + 1;
    
            for($current_page = 1; $current_page <= $total_page; $current_page++){
                if($max_entries * ($current_page - 1) < $count) {
                    $offset = $max_entries * ($current_page - 1);
                    $sql = "SELECT max(date_gmt) FROM {$wpdb->prefix}manga_chapters ORDER BY date_gmt DESC LIMIT {$max_entries},{$offset}";
                    $date = $wpdb->get_var($sql);
    
                    $index[] = array(
                            'loc'     => \RankMath\Sitemap\Router::get_base_url( 'wp-manga-chapters-sitemap'. $current_page .'.xml' ),
                            'lastmod' => $date,
                        );
                }
            }
    
            return $index;
        }
    
        /**
         * Get set of sitemap link data.
         *
         * @param string $type         Sitemap type.
         * @param int    $max_entries  Entries per sitemap.
         * @param int    $current_page Current page of the sitemap.
         *
         * @return array
         */
        public function get_sitemap_links( $type, $max_entries, $current_page ){
            $urls = array();
    
            global $wpdb, $wp_manga_functions;
    
            $offset = ($current_page - 1) * $max_entries;
    
            $sql = "SELECT * FROM {$wpdb->prefix}manga_chapters ORDER BY date_gmt DESC LIMIT {$offset}, {$max_entries}";
    
            $results = $wpdb->get_results($sql);
    
            global $_wp_manga_wpseo_sitemap;
            $_wp_manga_wpseo_sitemap = true;
            foreach($results as $result){
                $manga_id = $result->post_id;
    
                // check if $manga is active
                if(get_post_status($manga_id) == 'publish'){
                    $chapter_slug = $result->chapter_slug;
    
                    $link = $wp_manga_functions->build_chapter_url( $manga_id, $chapter_slug );
    
                    $urls[] = array(
                            'loc' => esc_url($link),
                            'mod' => $result->date_gmt,
                            'chf' => 'daily', // Deprecated, kept for backwards data compat. R.
                            'pri' => 1, // Deprecated, kept for backwards data compat. R.
                            'images' => array());
                }
            }
            $_wp_manga_wpseo_sitemap = false;
    
            return $urls;
        }
    }
    
    add_filter( 'rank_math/sitemap/providers', function( $providers ) {
    	$providers[] = new Madara_Sitemap_Provider();
    	return $providers;
    } );

    Hope that helps.

    Thread Starter oneoff

    (@oneoff)

    hello thank you for reply, unfortunately, didn’t work for me
    I think some function needs to change too
    like public function get_index_links
    and function get_sitemap_links

    this function is for yost seo and I’m not able to find rank math one

    Plugin Author Rank Math SEO

    (@rankmath)

    Hwllo @oneoff

    Sorry for the delayed response.

    It works correctly on our setup. Here’s a screencast:
    https://i.rankmath.com/Eg9bjR

    Can you please clear the cache, server & WordPress-side and then test?

    Looking forward to helping you. Thank you.

    Plugin Author Rank Math SEO

    (@rankmath)

    Hello,

    Since we did not hear back from you, we are assuming that you are using the latest version of all the plugins, themes, & WordPress, and the issue is resolved, we are closing this topic.

    If that is not the case, please feel free to reply to this topic or open a new one and we would be more than happy to assist.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘support custom post type in sitemap’ is closed to new replies.