• My mission is to overwrite the default post category.
    On the page where i can create a new post i have the parent ID in the URL like ?vis=92
    Then i want to make a function, so i can add the category related to the parent ID, to the new post.
    It means that i will overwrite the default category.
    Any ideas how this function should look like?

    I know its a wp subject now. Maybe i should move it to the wp forum?

Viewing 12 replies - 16 through 27 (of 27 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, of course, one thing at a time ??

    You still have a syntax error in save_cat’s if conditional –there are no quotes for ['term_id']. It’d be better to do this:
    if ( array_key_exists('term_id', $_POST )) {

    The hidden field would not be saved in any table, it’s inserted dynamically when the edit screen loads. If you want to verify it’s there, view the edit screen’s source HTML (ctrl/cmd+U often gets you there). Then search for “term_id”.

    Thread Starter michaelgoal

    (@michaelgoal)

    I use quotes for [‘term_id’] now.
    Im not used to work with the editor, and dont know where to find it. When i go to settings and writing there is no editor that i can choose.

    It seems, that i for some reason, dont change the value for term_id in the database.

    add_action( 'block_editor_meta_box_hidden_fields', 'add_cat', 10, 1 );
    function add_cat($post) {
     if (array_key_exists('vis', $_GET ) && '92'== $_GET['vis']) {
     echo '<input type="hidden" name="term_id" value="24">';
     }
    }
    
    add_action( 'save_post', 'save_cat' );
    function save_cat( $post_id ) {
     if ( array_key_exists('term_id', $_POST )) { 
     wp_set_post_terms($post_id,'24','category'); }
    }
    Moderator bcworkz

    (@bcworkz)

    Your latest code works correctly on my site. I do have to reload the page to see the assigned category. But it is correctly assigned in the DB. You probably misspoke when you said “[it] dont change the value for term_id in the database”, so to be clear, there is no “term_id” value being changed in the DB. What is happening is the term whose ID is 24 is being assigned to the current post, while unassigning any other terms that may have been assigned.

    When you open a new post for editing, the edit screen should look very similar to one of the two images shown under “The Editor’s Workspace” setion. The ‘block_editor_meta_box_hidden_fields’ action only works with that editor.

    Thread Starter michaelgoal

    (@michaelgoal)

    Nice that it works on your site. Thats a progress !
    Maybe the problem is that i use the Buddyoress User Blog plugin:
    https://www.buddyboss.com/product/buddypress-user-blog/

    I didnt know that it could be a problem.
    Do you know if there is a hidden_fields action for this editor too ?

    Yes i meant that the term with ID=24 is being assigned to the current post.

    Moderator bcworkz

    (@bcworkz)

    For alternative editors I recommend asking in their respective dedicated support forum. You need any action that fires within the editor’s <form> element from which you can output a hidden field. The devs and expert users in their forum would be in the best position to advise you on that.

    Thread Starter michaelgoal

    (@michaelgoal)

    Hi again,
    If i use the editor backend the screen looks like the one in your link.
    So maybe its ok..
    If you want to and have time to, you can try and login on my site : https://goaldigger.dk/
    user: [redacted] , password: [redacted]

    – and then click on the running woman(L?b)
    (in the middle below the large photo)

    – and then click on the text at the top “l?be 10 min”

    – and then hover “Add new blog” and you can see the url with “?vis=92”

    – when you click on this link you will come to the create new blog page.

    You can try to add a new blog and see that the category still is “ikke kategoriseret” after submitting and refreshing the page.
    Category should be “L?b”
    Doesnt it look like same editor as Gutenberg editor backend ?

    Happy new year

    • This reply was modified 4 years, 2 months ago by bcworkz.
    Thread Starter michaelgoal

    (@michaelgoal)

    I forgot that this is an open forum so i shouldnt write user and password here ??
    I have changed it now. If you want to test this issue i can send user and password.

    Moderator bcworkz

    (@bcworkz)

    What the editor looks like to me doesn’t change the situation. Is the requested file for that edit screen post-new.php? Assuming you use a somewhat recent WP version (5.0+), that file ends up requiring edit-form-blocks.php on line 72 (line numbers here may vary by version). Then in that file, it calls the_block_editor_meta_boxes() on line 434. That function in turn calls the_block_editor_meta_box_post_form_hidden_fields( $post ); (on line 2303 of wp-admin/includes/post.php) which fires the action from which your add_cat() callback outputs the hidden “term_id” field (line 2476 of the same file).

    You could temporarily place error_log() calls at key places to find out where in that sequence things are not going as expected.

    Thread Starter michaelgoal

    (@michaelgoal)

    Yes i have post-new.php and the other calls that you mention.
    In debug.log only error is:
    Trying to get property ‘post_parent’ of non-object in /home/www/goaldigger.dk/wp-content/plugins/buddyboss-platform/bp-forums/functions.php on line 841

    But it seems like its something with groups. Text an line 841 (Strong) is this:
    This loop works our way up to the top of the topic->sub-forum->parent-forum hierarchy..
    // We will stop climbing when we find a forum_id that is also the id of a group’s forum..
    // When we find that, we’ve found the group, and we can stop looking..
    // Or if we get to the top of the hierarchy, we’ll bail out of the loop, never having found a forum.
    // that is associated with a group..

    while ( ! $found_group_forum && ! $reached_the_top ) {
    		$forum_group_ids = bbp_get_forum_group_ids( $forum_id );
    		if ( ! empty( $forum_group_ids ) ) {
    			// We've found the forum_id that corresponds to the group's forum.
    			$found_group_forum = true;
    			$group_id          = $forum_group_ids[0];
    		} else {
    			$current_post = get_post( $forum_id );
    			<strong>if ( $current_post->post_parent ) {</strong>
    				// $post->post_parent will be the ID of the parent, not an object.
    				$forum_id = $current_post->post_parent;
    			} else {
    				// We've reached the top of the hierarchy.
    				$reached_the_top = true;
    			}
    		}
    	}

    ————————————–
    Im not experienced enough with wp code to know where it would be smart to place the error_log().. If i could i would test if this is true:
    if (array_key_exists('vis', $_GET ) && '92'== $_GET['vis']) {
    and why term_id is not set to 24:
    echo '<input type="hidden" name="term_id" value="24">';

    Damn im tired of fighting with this..WP code is very complex to me and difficult to see through..

    Did you test with exactly this code:

    add_action( 'block_editor_meta_box_hidden_fields', 'add_cat', 10, 1 );
    function add_cat($post) { 
     if (array_key_exists('vis', $_GET ) && '92'== $_GET['vis']) {
    	 echo '<input type="hidden" name="term_id" value="24">';
     }
    }
    
    add_action( 'save_post', 'save_cat' );
    function save_cat( $post_id ) {
     if ( array_key_exists('term_id', $_POST )) { 
     wp_set_post_terms($post_id,'24','category'); }
    }
    Moderator bcworkz

    (@bcworkz)

    I’d put an error_log() call just before the if() conditional just to be see if the callback is even called or not. If it is, and yet the input field does not exist in the edit screen’s source HTML, then there’s an issue in the checking of $_GET.

    Well, the code I tested wasn’t exactly that, I don’t have a term ID 24, but I do have one as 22. That’s all I changed. I manually added &vis=92 to the URL since I’ve no code to add that automatically. One big difference between our sites is I don’t have BuddyBoss. I suspect if you deactivated that plugin and tested your code on a regular post like I did, that the chosen category will be set as you intended. You may not even need to deactivate the plugin, just test on a regular post.

    Another thought — are you sure the BuddyBoss categories are really the category taxonomy? Some plugins have categories that are not category, but a custom taxonomy. For example, WooCommerce’s categories for products are simply labeled “category”, but the taxonomy is actually product_category. The actual taxonomy is in the URL for the the category’s back end list table. For example /wp-admin/edit-tags.php?taxonomy=category.

    The error about non-objects is a bit concerning. $current_post is what is a non-object, but it should be a WP_Post object. Where it’s assigned is somewhere before the snippet you posted. The code is trying to find the top level ancestor of the current post, which I imagine for BuddyBoss is that post’s group. The group is neither here nor there, the issue is in assigning non-objects to $current_post. Our code wouldn’t have anything to do with actual WP_Post objects, so something else is amiss there.

    WP code is complex to everyone, you’re not alone. It’s too much to fully grasp as a whole. Having a general idea of what happens with requests is the best you can hope for. When it comes to customizing, I try to find the relevant source code (not always easy), and focus just on what I see right there. Sometimes I need to dig deeper into other referenced code. Since most customization involves action and filter hooks, you mainly need to be concerned only with what data is passed to your callback and what WP expects in return. What WP does before and after is a factor only for determining if we’ve found the right action or filter hook. The rest of WP doesn’t really matter.

    Thread Starter michaelgoal

    (@michaelgoal)

    hi again,

    I have these tables and data with this post:
    wp_posts (table)
    ID: 353
    post_status: public
    post_type.post
    ———————————

    Wp_term_relationships (table)
    object_id: 353
    term_taxanomy_id: 1
    term_order: 0
    ——————————–
    wp_term_taxanomy (table)
    term_taxanomy_id: 1
    term_id: 1
    taxanomy: category

    term_taxanomy_id: 24 (another category)
    term_id: 24
    taxanomy: category

    term_taxanomy_id: 25 (another category)
    term_id: 25
    taxanomy: category

    ————————–

    wp_terms (table)
    term_id: 1
    name: ikke-kategoriseret
    slug: ikke-kategoriseret

    term_id: 24
    name: l?b
    slug: loeb

    term_id: 25
    name: rygestop
    slug: rygestop
    ——————————-
    It is category isnt it ?

    Post with ID=53 should have term_taxanomy_id: 24 and then term_id:24 and name: l?b
    but it just has default values like:
    term_taxanomy_id: 1 and then term_id:1 and name: ikke-kategoriseret

    Is it before this if:
    if (array_key_exists('vis', $_GET ) && '92'== $_GET['vis']) {
    you would put an error_log ?
    And what should i put into the error_log() braces ?
    Or can i try to print something if its true ? and how ?

    Thread Starter michaelgoal

    (@michaelgoal)

    Did you give up on this issue?
    Its ok but though nice to know??

Viewing 12 replies - 16 through 27 (of 27 total)
  • The topic ‘Overwrite default post category’ is closed to new replies.