Yoast / wordpress-seo

Yoast SEO for WordPress
https://yoast.com/wordpress/plugins/seo/
Other
1.77k stars 891 forks source link

"Primary Term" on Custom Post Types not working #13757

Open sara-p- opened 4 years ago

sara-p- commented 4 years ago

Please give us a description of what happened.

When multiple categories or custom taxonomies are selected for a custom post type, clicking on "Make primary" does nothing.

The Custom Post Type is set to "'publicly_queryable' => true", and the Metabox for the categories and custom taxonomies are set to "Show".

I've tested this on 2 themes, including the standard WordPress 2019 theme, and run into the same issue.

I've deactivated all plugins other than yoast, and it still will not work.

The only way I can get it to work is by rolling back to Yoast 8.0

Please describe what you expected to happen and why.

Clicking on "Make Primary" should bold the primary term (and set that term as primary).

How can we reproduce this behavior?

  1. Create Custom Post Type
  2. Set "'publicly_queryable' => true,", and Metabox to "show"
  3. From "Edit Post", select more than one category, and try to set one of the terms as primary by clicking "Make primary"

Technical info

Used versions

KustomKode commented 4 years ago

There are no javascript errors.... it just doesn't do anything when you click on the button with the class "wpseo-make-primary-term", like it has no functions attached to the .on('click') event

Djennez commented 4 years ago

Hi @sara-p- and thank you for your report.

Could you please provide more details, as I am currently unable to reproduce your issue. I have registered a new post type with a category taxonomy and the primary category functionality seems to work without issue:

image

Maybe provide the code used to register the post-type in question along with its taxonomies / terms.

Note that there is this issue with the primary category when the Yoast metabox is set to hide. But that makes the primary category fail in Gutenberg as well.

sara-p- commented 4 years ago

Hi @Djennez, thanks for your reply and sorry for the delay.

Here's the code for the CPT: `<?php function people() {

    $labels = array(
        'name'                  => 'People',
        'singular_name'         => 'People',
        'menu_name'             => 'People',
        'name_admin_bar'        => 'People',
        'archives'              => 'People Archives',
        'attributes'            => 'People Attributes',
        'parent_item_colon'     => 'Parent Person:',
        'all_items'             => 'All People',
        'add_new_item'          => 'Add New Person',
        'add_new'               => 'Add Person',
        'new_item'              => 'New Person',
        'edit_item'             => 'Edit Person',
        'update_item'           => 'Update Person',
        'view_item'             => 'View Person',
        'view_items'            => 'View People',
        'search_items'          => 'Search People',
        'not_found'             => 'Not found',
        'not_found_in_trash'    => 'Not found in Trash',
        'featured_image'        => 'Featured Image',
        'set_featured_image'    => 'Set featured image',
        'remove_featured_image' => 'Remove featured image',
        'use_featured_image'    => 'Use as featured image',
        'insert_into_item'      => 'Insert into item',
        'uploaded_to_this_item' => 'Uploaded to this item',
        'items_list'            => 'Items list',
        'items_list_navigation' => 'Items list navigation',
        'filter_items_list'     => 'Filter items list',
    );
    $args = array(
        'label'                 => 'People',
        'description'           => 'Stores and displays people profiles.',
        'labels'                => $labels,
        'supports'              => array( 'title' ),
        'hierarchical'          => false,
        'public'                => false,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-id-alt',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'people', $args );

}
add_action( 'init', 'people', 0 );

function add_taxonomies_to_people() {
    register_taxonomy_for_object_type( 'post_tag', 'people' );
}

add_action( 'init', 'add_taxonomies_to_people' );

`

Here's the specific custom taxonomy we need to work with Primary Term:

`<?php function positions() {

    $labels = array(
        'name'                       => 'Positions',
        'singular_name'              => 'Position',
        'menu_name'                  => 'Positions',
        'all_items'                  => 'All Positions',
        'parent_item'                => 'Parent Position',
        'parent_item_colon'          => 'Parent Position:',
        'new_item_name'              => 'New Position',
        'add_new_item'               => 'Add Position',
        'edit_item'                  => 'Edit Position',
        'update_item'                => 'Update Position',
        'view_item'                  => 'View Position',
        'separate_items_with_commas' => 'Separate items with commas',
        'add_or_remove_items'        => 'Add or remove items',
        'choose_from_most_used'      => 'Choose from the most used',
        'popular_items'              => 'Popular Items',
        'search_items'               => 'Search Items',
        'not_found'                  => 'Not Found',
        'no_terms'                   => 'No items',
        'items_list'                 => 'Items list',
        'items_list_navigation'      => 'Items list navigation',
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => false,
        'show_tagcloud'              => false,
    );
    register_taxonomy( 'positions', array( 'people' ), $args );

}
add_action( 'init', 'positions', 0 );
`

There are several other custom taxonomies that we are using. Let me know if you would like those as well.

Thanks!

sara-p- commented 4 years ago

I FIGURED IT OUT!

In the CPT args, "public" MUST be set to "true".

KustomKode commented 4 years ago

@sara-p-, I hope you are correct, because that seems like an easy fix! I will try this sometime in the next 24 hours on the site of mine that I was having the issue.

KustomKode commented 4 years ago

I just tried it...... IT WORKED!

Awesome find @sara-p-

I wonder why public could be set to false on previous versions?

KustomKode commented 4 years ago

I can't believe how many hours I invested into this... for such a simple fix.... lol

sara-p- commented 4 years ago

@KustomKode you and me both.

Djennez commented 4 years ago

Hmmm, good find! I am not sure if this function should work with non-public CPTs, but if it shouldn't, at least it should display something else (either remove the function or show a warning).

KustomKode commented 4 years ago

I DO believe it should work with non-public CPT's, just like it used to.... What does public or non-public have to do with what category you want to query a post by?

KustomKode commented 4 years ago

The functionality changed with the update that added the ability to "make primary" with Gutenberg.

noahkuhn commented 4 years ago

Still seeing this. Yoast 12.6.2, WP 5.3. Clicking "Make Primary" doesn't do anything. I am using CPT UI to create my taxonomies, but "public" and "publicly_queryable" are both true so it seems there is another reason to this? Currently my site is broken due to this bug.

stefanfisk commented 4 years ago

I'm also seeing this, and can confirm that when I make the post type public the primary category controls start working.

stefanfisk commented 4 years ago

It's not entirely easy to debug minified code, but I was able to find that something is going wrong in the function pasted below. Instead of toggling the correct category it seems to enabled and then disable the clicked row.


function h(n) {
var o, i, a;
o = e("#" + n + 'checklist input[type="checkbox"]:checked');
var u = e("#" + n + "checklist li");
u.removeClass("wpseo-term-unchecked wpseo-primary-term wpseo-non-primary-term"),
e(".wpseo-primary-category-label").remove(),
u.addClass("wpseo-term-unchecked"),
o.length <= 1 || o.each(function(o, u) {
    u = e(u),
    (i = u.closest("li")).removeClass("wpseo-term-unchecked"),
    function(t) {
    return 1 === e(t).closest("li").children(".wpseo-make-primary-term").length
    }(u) || function(r, n) {
    var o, i;
    o = e(n).closest("label"),
    i = t({
        taxonomy: f[r],
        term: o.text()
    }),
    o.after(i)
    }(n, u),
    u.val() === d(n) ? (i.addClass("wpseo-primary-term"),
    (a = u.closest("label")).find(".wpseo-primary-category-label").remove(),
    a.append(r({
    taxonomy: f[n]
    }))) : i.addClass("wpseo-non-primary-term")
})
larssn commented 4 years ago

Please fix this, it is also broken in WooCommerce's product categories.

So you can't currently set the primary category for products with multiple categories, which is pretty serious.

Djennez commented 4 years ago

@larssn you might be running in another issue, as this thread is about non-public posttypes. I am unable to reproduce this problem with a default installation with WooCommerce.

nuvoPoint commented 4 years ago

We have somehow the same issue.

Active theme:

Active plugins:

Issue: Nothing happens when you click on "Make primary" and normally the first one would have been marked as "Primary". primary

This only happens when this option is set to "hide": Issue

Djennez commented 4 years ago

@nuvoPoint that's a known issue and described in https://github.com/Yoast/wordpress-seo/issues/13032

Xriuk commented 4 years ago

We have somehow the same issue.

Active theme:

* Twenty Nineteen

Active plugins:

* WooCommerce

* Yoast SEO

Issue: Nothing happens when you click on "Make primary" and normally the first one would have been marked as "Primary". primary

This only happens when this option is set to "hide": Issue

This was the problem in my case, as soon as I showed the Yoast SEO Meta Box, it began working again

rmarcano commented 4 years ago

Please inform the customer of conversation # 620464 when this conversation has been closed.

nloenne commented 4 years ago

I have the same problem and would love an update once it's solved.

My current workaround is to set the CPT to public and then use Yoast to set it to noindex and remove from XML Sitemap - but I'd like to know when I can set the CPT to non-public again without losing the option to change primary category.

ValeriePintar commented 5 months ago

Years later - I agree with @nloenne. I have functionality that requires both my custom post types to have primary categories and also for them to have 'public' set to 'false' so that there are no public facing URLs. An update here would be appreciated!