• Resolved shanebp

    (@shanebp)


    I have a cpt with front-end creation on the front page.
    Using a 2013 child theme.

    Using wp_dropdown_categories to output the cats and the output is correct:

    <select name="cat" id="cat" class="postform">
    	<option value="-1">Select Category</option>
    	<option class="level-0" value="1">Uncategorized</option>
    	<option class="level-0" value="4">National</option>
     etc.

    After inserting the post, I do this:

    $selected_cat = array( $_POST['cat'] );
    wp_set_post_terms($post_id, $selected_cat, 'category');

    Everything works except when I submit I end up on the ‘Nothing Found’ page.

    But the cpt is created and assigned to the selected category.

    But when I go to a listing of all the created cpts, and click the assigned cat, I again end up on the ‘Nothing Found’ page.

    Looking in the _term_relationships table, I see that the cpt ID and term_taxonomy_id are correct.

    In wp_admin, in the cpt tab, the cpts are listed with the correct cat
    and clicking one shows only those cpts in the same cat.

    If I go to Posts > Categories, it shows zero posts in all categories.

    If I remove the cat selector from the creation form, and do a submit, it refreshes to the page and gives me the success message.

    In register_post_type, I have:

    ...
    		'taxonomies' => array('category'),
    		)
    	);
    	register_taxonomy_for_object_type('category', 'mycpt');

    So I’m doing something wrong re the cat selector… or… ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter shanebp

    (@shanebp)

    Ok, seems there are 2 separate things happening…

    1. Using a child theme of WP 2013 – if I submit a custom post from the home page and use wp_dropdown_categories, WP thinks I’m searching and refreshes to the assigned category archive page.
    If I use wp_terms_checklist(), it refreshes back to the home page.

    2. Custom posts are assigned to the selected category, but are not listed on that category page.

    Moderator bcworkz

    (@bcworkz)

    You’ve correctly identified the problems. What to do about them?

    For the category search, where ever the form is submitted, I think it is seeing the ‘cat’ field name and responding to it as a request for a category archive. You can probably stop this behavior by simply supplying a different ‘name’ argument that will not be seen as a category archive request. At least I think so. The function does not generate requests on it’s own, something else is. Not knowing what that is means I can’t be sure how to respond. The ‘name’ change is a good guess though.

    The default category archive request only queries for post post types by default. In order to query for other post types, hook into the ‘pre_get_posts’ action and set the query var ‘post_type’ as an array containing all post types you want to query for. This would be applied to all queries. In order to limit it’s application only set the query var if certain conditions apply. Perhaps only if is_category() is true. Or maybe only for certain categories. There’s usually a way to limit application to any set of arbitrary conditions.

    Thread Starter shanebp

    (@shanebp)

    Thanks bcworkz.

    Yes, I forgot about the need for a pre_get filter on cpt cats, duh.

    The unexpected category search on submit is solved by putting the page permalink in the form action field.
    http://wordpress.stackexchange.com/questions/113090/custom-post-creation-on-front-page-on-submit-a-search-is-done

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

The topic ‘cpt & categories’ is closed to new replies.