-
Hi,
I’ve moved from Yoast to RankMath and all my posts lost the primary category setting. They were all set to the default “Blog” category.
Now, in the editor, I do see the option to “Make the Term Primary” on the Categories list, but once I set it and update the post, nothing happens, it still continues to show as “Blog”.
Can you help please?
-
Hello,
Thank you for contacting the support and sorry for any inconvenience that might have been caused due to that.
Assuming you are using the latest version of all the plugins and the themes (please update if not yet), it would seem like a conflict with one of the plugins or the theme you are using.
To determine that, please follow this troubleshooting guide:
https://rankmath.com/kb/check-plugin-conflicts/Only the site admin would know and your users won’t be affected by this troubleshooting.
If the issue persists, please record a video screencast using a tool like Loom showing the troubleshooting and edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.
It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:
https://wordpress.org/plugins/temporary-login-without-password/
You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):
https://wordpress.org/plugins/wp-security-audit-log/
Please do take a complete backup of your website before sharing the information with us.
We really look forward to helping you.
I realized this has to do with my custom code implementation which was made to grab the primary category of a post as defined by Yoast.
This is the current code.
Can you let me know how can I replicate this same function to grab the primary category as defined by Rankmath? Thanksfunction get_primary_taxonomy_id( $post_id, $taxonomy ) { $prm_term = ''; if (class_exists('WPSEO_Primary_Term')) { $wpseo_primary_term = new WPSEO_Primary_Term( $taxonomy, $post_id ); $prm_term = $wpseo_primary_term->get_primary_term(); } if ( !is_object($wpseo_primary_term) && empty( $prm_term ) ) { $term = wp_get_post_terms( $post_id, $taxonomy ); if (isset( $term ) && !empty( $term ) ) { return wp_get_post_terms( $post_id, $taxonomy )[0]->term_id; } else { return ''; } } return $wpseo_primary_term->get_primary_term(); }
Hello,
Thank you so much for getting in touch.
Can you check if you can select your primary categories when you edit your post like the screenshot below?
Thank you, and looking forward to your update.
Hi Roel.
Thanks for getting in touch.
I’m able to change and save the primary category using that dropdown, but I’m displaying the post primary category on the frontend using the function I shared above. Because it’s using Yoast’s variable it doesn’t work now. How can I replicate it to RankMath? I believe it has do with “WPSEO_Primary_Term”?
Thanks!
Hello,
We apologize for the delay and any inconvenience this may cause.To display the primary category name for the post, please place the following function in your Themes > functions.php
/** * Display Primary Category Name of post * @param $term string The term name * @param $postID int The post ID */ function RM_get_post_primary_category($post_id, $term='category', $return_all_categories=false){ $return = array(); if (class_exists('WPSEO_Primary_Term')){ $wpseo_primary_term = new WPSEO_Primary_Term( $term, $post_id ); $primary_term = get_term($wpseo_primary_term->get_primary_term()); if (!is_wp_error($primary_term)){ $return['primary_category'] = $primary_term; } } if (empty($return['primary_category']) || $return_all_categories){ $categories_list = get_the_terms($post_id, $term); if (empty($return['primary_category']) && !empty($categories_list)){ $return['primary_category'] = $categories_list[0]; //get the first category } if ($return_all_categories){ $return['all_categories'] = array(); if (!empty($categories_list)){ foreach($categories_list as &$category){ $return['all_categories'][] = $category->term_id; } } } } return $return; }
To Display the Primary category name for the post at the front-end use the following snippet
// get the post ID $post_id = get_the_ID(); $post_categories = RM_get_post_primary_category($post_id, $term='category', $return_all_categories=false); // Display Primary category name echo $post_categories['primary_category']->name;
If you require any further assistance, please feel free to contact us.
Looking forward to your update. Thank you.Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.
The ticket ‘Can’t update primary category’ is closed to new replies.