• Resolved christianl86

    (@christianl86)


    Is it possible to use the primary category? The plugin takes the category by alphabetical order but primary should have the highest priority. I think you need to improve that. Thank you! ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter christianl86

    (@christianl86)

    I just replaced the file -> frontend/class-permalinks-customizer-form.php:

    // Replace %category% with the respective post category with their parent categories
    		if (strpos( $replace_tag, "%category%" ) !== false ) {
    			$categories = get_the_category( $post_id );
    			$total_cat  = count( $categories );
    			$tid = 1;
    			if ( $total_cat > 0 && is_array( $categories ) ) {
    				$tid = '';
    				foreach ( $categories as $cat ) {
    					if ( $cat->term_id < $tid || empty( $tid ) ) {
    						$tid = $cat->term_id;
    						$pid = '';
    						if ( ! empty( $cat->parent ) ) {
    							$pid = $cat->parent;
    						}
    					}
    				}
    			}
    			$term_category = get_term( $tid );
    			$category      = is_object( $term_category ) ? $term_category->slug : '';
    			if ( ! empty( $pid ) ) {
    				$parent_category = get_term( $pid );
    				$category        = is_object( $parent_category ) ? $parent_category->slug . '/' . $category : '';
    			}
    			$replace_tag = str_replace( '%category%', $category, $replace_tag );
    		}

    TO

    // Replace %category% with the respective post category with their parent categories
    		if (strpos( $replace_tag, "%category%" ) !== false ) {
    			$categories = get_the_category( $post_id );
    			
    			global $wpdb;
    			$query = "SELECT * FROM <code>&quot;.$wpdb->prefix.&quot;postmeta</code> WHERE <code>post_id</code> = ".$post_id." AND <code>meta_key</code> = '_yoast_wpseo_primary_category'";
    			$res = $wpdb->get_results($query);
    			$tid = $res[0]->meta_value;
    
    			$term_category = get_term( $tid );
    			$category      = is_object( $term_category ) ? $term_category->slug : '';
    			if ( ! empty( $pid ) ) {
    				$parent_category = get_term( $pid );
    				$category        = is_object( $parent_category ) ? $parent_category->slug . '/' . $category : '';
    			}
    			$replace_tag = str_replace( '%category%', $category, $replace_tag );
    		}

    And it works, I need to know how to regenerate the url to posts created before install this plugin… “Convert Custom Permalink” isn’t working for me.

    Plugin Author Sami Ahmed Siddiqui

    (@sasiddiqui)

    Hi @christianl86

    Thanks for providing your solution. First issue in your code is that we can’t rely on the Yoast Plugin. Maybe any user doesn’t use that. I know that Yoast is one of the most popular plugin and millions of users but i can’t rely on it.

    Secondly, i have tried to match category structure with the WordPress Default Structure. Have you tried the WordPress Default that what does they do. Does WordPress gives the priority to the Primary Category? Try that out and let me know if WordPress gives the priority to the Primary Category.

    Thanks,
    Sami

    Plugin Author Sami Ahmed Siddiqui

    (@sasiddiqui)

    As per not getting any response from more than a month, i am marking this thread as resolved. If anyone have any issue so, please feel free reopen the thread or post a new thread.

    Thanks,
    Sami

    What would be better is a filter to change the %category% and NOT editing the plugin directly…

    Plugin Author Sami Ahmed Siddiqui

    (@sasiddiqui)

    Hi @unaomnia

    Why you need a filter for category? Any specific reason for this?

    Thanks,
    Sami

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Primary Category’ is closed to new replies.