Skip to:
Content
Pages
Categories
Search
Top
Bottom

Creating a reply programmatically, it doesn't appear in the forums


  • cjerrells
    Participant

    @cjerrells

    Hi,

    I’m running the latest bbPress (2.5.4) and using Gravity Forms and the Custom Post Types addon to automatically create a bbPress reply when somebody submits a form. This is mostly working – the reply post is created in WordPress, I’m setting the _bbp_reply_to, _bbp_forum_id and _bbp_topic_id meta fields and it looks like it should in the backend.

    However it doesn’t appear as a reply under the topic when you view the forums.

    If I edit the newly-created reply in the WordPress admin backend (just hitting Update without making any changes) it does then appear.

    I presume there’s some additional step required to update the forums metadata or relationships? I’ve tried using the “repair forum” tools after creating the reply, and calling bbp_update_reply from a hook on form submission, but neither seem to make the reply appear. The only thing which does is clicking “Update” in the backend.

    Any pointers would be very much appreciated! It feels like I’m 90% there but am missing some vital “refresh” step at the end.

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

  • Robin W
    Moderator

    @robin-w

    I can only suggest that you create a new topic using your programme

    Then I’d look at everything in the database in wp_posts & wp_postmeta that has that ID

    Take a copy of all of this

    Then hit the update in the backend and see what has changed


    cjerrells
    Participant

    @cjerrells

    Thank you for the suggestion! This has helped me find the problem.

    Gravity Forms lets you set custom fields on posts created, but not all of the core post properties. So it turns out that my approach was creating a new custom field called “post_parent” rather than actually setting the “post_parent” property of the post.

    I’m not sure why re-saving the post in the backend solved this issue, but I’ve now set up a hook which monitors for new posts and if a particular custom field is set, then sets the “post_parent” property correctly, and my auto-generated replies are now showing up in the thread as expected. Great! Thanks again.


    Robin W
    Moderator

    @robin-w

    Great – glad you’re fixed, and thanks for posting what you did, this will help anyone looking at this thread !


    jsteckler1
    Participant

    @jsteckler1

    cjerrels,

    I have essentially wound up at the same point as you, except saving on the backend isn’t what put (in my case) the topic in the correct forum, but adding a reply to the topic did.

    What is the hook you used to “monitor for new posts and if a particular custom field is set, then sets the “post_parent” property correctly?”

    @jsteckler1,
    I have no idea on how things were setup, but just to add my idea:
    – On save_post, launch your own routine
    – in your routine, if it’s a topic, set the post_parent to the forum ID (so _bbp_forum_id)
    – in your routine, it it’s a reply, set the post_parent to the topic ID (so _bbp_topic_id)


    cjerrells
    Participant

    @cjerrells

    @jsteckler1: We use GF to set a custom field called “_mu_autogenerated_post_to” and then our code looks like this:

    
    /*
     * Small fix for posting new introductions to the introductions thread
     * Would be nice to use the GF hook but post_id not set with Custom Post Types addon -_-
     */
    function set_post_parent($post_id)
    {
        if (get_post_meta($post_id,"_mu_autogenerated_post_to",true))
        {
            remove_action( 'save_post', 'set_post_parent',10); // Avoid infinite loop when wp_update_post calls save_post
            $target_discussion = get_post_meta($post_id,"_mu_autogenerated_post_to",true);
            error_log("Detected new _mu_autogenerated_post_to " . $target_discussion);
            wp_update_post( array('ID' => $post_id,'post_parent'=>$target_discussion) );
            error_log("Updated post parent.");
            add_action( 'save_post', 'set_post_parent',10);
        }    
        else
        {
           // error_log("Ignoring non-_mu_autogenerated_post_to post creation");
        }
    }
    add_action( 'save_post', 'set_post_parent',10);
    

    Hope that helps!


    jsteckler1
    Participant

    @jsteckler1

    Hi @casiepa,

    Thank you for jumping in!

    Some additional details about my situation:

    Upon submission of a specified Gravity Form, I am attempting to create a new topic under a specified forum (Forum ID: 31086).

    While creating the new Topic was simple enough with the Custom Post add-on—which successfully created the Topic, including the body of the topic post, accordingly—the topic was being listed under “No Forum.” @cjerrells post got me closer, encouraging me to add another custom field named _bbp_forum_id with a default value of 31086 (the forum ID). After doing that, the assigned forum seemed to display correctly on the backend, but:

    1) The breadcrumb would only display the forum after editing and resaving the post again

    — It still will not, however, show on the front-end forum among the other forum topics

    2) The topic would finally display correctly on the front-end among the other forum topics only after adding a reply to the topic post.

    Here is a screenshot of my Gravity Form settings, as well as the code cjerrells kindly provided within my functions.php file. My ability to write php is still very elementary, and it seems a bit difficult to find a full list of bbPress actions and hooks, so I wasn’t sure exactly what needed to be altered to fit my needs.

    I would be beyond appreciative if you could give it all a look and get back to me with any thoughts or ideas you may have.


    jsteckler1
    Participant

    @jsteckler1

    Hi @casiepa,

    I have failed in my attempt to set post_parent to _bbp_forum_id. Could you help me build on the code provided by @cjerrells perhaps? Though he was focused on replies, we were both experiencing the same issue on the front-end.

    Or, alternatively, I am only using ONE forum. Is there any code I could use to force ALL topics created to be sent to a specified forum?


    Robin W
    Moderator

    @robin-w

    I think I have code that fixes this in

    Gravity Forms to bbPress (Force Topics to Specified Forum)

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
Skip to toolbar