Programmatically set the primary category

#758566
  • Resolved Anonymous
    Rank Math free

    Hi there!

    We have a website that is pulling its data from a third party api service so we have created an importer and all works okay, the only issue we have is setting the primary category during the import as there is quite a few products 500+ and each will need a primary category setting for the breadcrumbs to be consistent across the site.

    Your chatbot suggested using the following:


    update_post_meta($post_id, 'rank_math_primary_category', (int)$parent_category_term->term_id);

    However we have had no success.

    The full code is here:

    // Set Parent Category Taxonomy
    if ( ! empty($product['Catagory']['Level1'])) {
    $parent_category_term = Helpers::setPostTerm(
    $product['Catagory']['Level1'],
    'product-categories',
    (int)$post_id
    );

    // Update Rank Math Primary Category
    update_post_meta($post_id, 'rank_math_primary_category', (int)$parent_category_term->term_id);
    }

    The actual creation and assignment of the category taxonomy works fine, it is only the marking of the primary category to rank math that is the issue.

    Any help would be greatly appreciated.

    Thanks,
    Jordan

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    I apologize for the issue you are facing with setting the primary category programmatically in Rank Math during the import process. To set the primary category for Rank Math, you can use the following code snippet:

    update_post_meta($post_id, 'rank_math_primary_category', (int)$parent_category_term->term_id);

    In your provided code snippet, it seems like you are already using this code to update the primary category for Rank Math. Since you mentioned that the creation and assignment of the category taxonomy is working fine but marking the primary category in Rank Math is the problem, I recommend checking the following:

    1. Ensure that $parent_category_term is correctly fetching the parent category term.
    2. Verify that the $post_id is correctly passed and corresponds to the post you are trying to update.
    3. Double-check that the rank_math_primary_category meta key is being set correctly with the term ID.

    If after verifying these points, the issue persists, please ensure that you are using the correct post ID and that the term ID is being retrieved accurately before updating the rank_math_primary_category meta key.

    If you continue to experience difficulties, feel free to reply here, and a human agent from our support team will assist you further.

    Thank you for reaching out with your concern.

    Best regards.

    Sources:
    How to Automate the Insertion of Focus Keyword » Rank Math
    How to Choose a Primary Category » Rank Math

    Anonymous
    Rank Math free

    Turns out the database is being updated, however visually within the CPT the dot is not selected next to the term

    Hello,

    You seemed to have marked this ticket as resolved.

    Do you still need our assistance with anything else or we can go ahead to close the ticket?

    We really look forward to helping you.

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this ticket.