Skip to main content

Can you check my code to update a space post

Hi everyone, I am using Flowmattic and some Fluentsnippets, trying to update some image downloads and posts. The following function is on a snippet and is called with a parameter with the updated content for a post. I match the title of the post and It is returning success, but for the life of me I can't get the update to "save". The post remains the same. Flowmattic is passing in the parameters.

Is there something I need to do to save the update?

Alternately, should I be using the API and if so, guidance on doing an update or delete? Thanks :)

use FluentCommunity\App\Models\Feed;

function fm_update_existing_post_matching_postname($postname, $MessageUpdate) {
    //$MessageUpdate = "Hello";

    // Safety check: Ensure Fluent Community is active
    if (!class_exists('FluentCommunity\App\Models\Feed')) {
        return 'FluentCommunity not found.';
    }

    // 1. Find the first post that matches the title
    $feed = Feed::where('title', $postname)
                ->where('status', 'published')
                ->first();

    // 2. If found, update the message
    if ($feed) {
        $feed->update([
            'message' => $MessageUpdate
        ]);

        // return true; // Success
        return $MessageUpdate;
    }

    return false; // No match found
}

Thomas Oates

Maybe add the following after updating the message.

$feed->save();

Jamie Robe

Thomas OatesΒ Thanks. That seems to be part of the issue, but now I see a different problem than what I thought was incomplete code or cache busting problems. I just updated my ticket with them and posted a very controlled experiment. No images, just a simple space post called Test1 with a text message saying message1.

I then ran the following hardwired function, looking for any number of posts with that same title and changing the message to Message22

function fm_update_existing_post_matching_postname($postname, $MessageUpdate) {
    $postname = "Test1";
    $MessageUpdate = "Message22";

    // 1. Find the feed(s) where the title matches
    // Note: 'title' is a column in the fcommunity_feeds table
    $feeds = Feed::where('title', $postname)->get();

    if ($feeds->isEmpty()) {
        return 0;
    }

    foreach ($feeds as $feed) {
        // 2. Update the message property
        $feed->message = $MessageUpdate;

        // 3. Persist to database
        $feed->save();
    }

    return $feeds->count();
}

I ran and no change! So I opened in incognito tab, no change. I purged my cloudflare, no change. I turned cloudflare to dev mode with no cache, no change!

I did an inspection with dev tools in chrome, and I see the original message1 in the html.

BUT, I clicked on the menu of the post itself, clicked edit, and I see the update I attempted in the code, Message22 is in the editor!!!!!!

I found if I manually clicked the update button, it will update to the new Message22 value.

So I am at a dead-end until I hear back from the support ticket. It must be somehow saved in some holding value in the database OR there needs to be some kind of "push" command I don't know of. It was not a caching issue, which threw me off all week.

I think I am not doing some step... but I don't know how to proceed. I have learned a huge amount:

  1. I went from just using my knowledge of PHP and using FluentSnippets (I love that plugin), all the way to trying Flowmattic (love it too).
  2. I have Flowmattic workflow, as well as a flowmattic table now, with my own snippets to do various things.
  3. There is an integration with FluentCommunity - but currently it only can create a new post (that works great), but no update or delete of a post yet.
  4. There are very good dev docs and sample snippets and so on, BUT we could use more real-world examples. I will try to donate things I come up with if they will take them. Not sure how open source they are. If not, I might post things somewhere else or share here.

Anyway, this is easy to replicate if anyone wants to try :)

I will let you all know what ends up working (I hope).

Peace, Jamie

Thomas Oates

Jamie RobeΒ Try setting $feed->message_rendered in addition to $feed->message.

Jamie Robe

Thomas OatesΒ It worked!!!!!!! I added the line you suggested below the other and the post message visibly updated!!!!!

    foreach ($feeds as $feed) {
        // 2. Update the message property
        $feed->message = $MessageUpdate;
        $feed->message_rendered = $MessageUpdate; // test

        // 3. Persist to database
        $feed->save();
    }

I will add this to my official ticket and try to get clarity from them as to how this all works.

Thomas, not sure where you are located but I own you a coffee or other beverage :)

Tomorrow I will work with my original functions and get the images working properly. This seemed like a 'simple' project at first, but between my ignorance of the proper way of doing the fluentcommunity code, my longer history of writing WP PHP plugins (where shortcodes work lol), and the actual issue of making a cache buster on the end of my image urls. This is way AI can help, but a human friend like Thomas who spends the time to answer is worth a 1000 AI's.

Peace, Jamie

Thomas Oates

Also, is using update() the proper way to update the message property of the feed?

Jamie Robe

Ok, I got a nice reply on my ticket, and it is confirmed that we must do both the message and message_rendered, then the save. If you look at the database tables themselves you will see both as fields in the wp__fcom_posts table.

Note that if you type in text in the editor itself, the message field will be that text. The second field has html added. In my case, I am creating or updating the posts programmatically and am just putting in html to both.

I now have most things working. However, I do not know how to set 2 things:

  1. How to disable comments programmatically.

  2. How to set the topic. I can't find the documentation on this anywhere. In the DB I do see the table that holds the topics and also a table that has the post-topic relationship.

    I tried this code for topics BUT not working:

        // This MUST happen after save() because it needs the Feed ID
        if ($topicId) {
            try {
                // Ensure the relationship exists before calling sync
                if (method_exists($feed, 'topics')) {
                    $feed->topics()->sync([$topicId]);
                }
            } catch (\Exception $e) {
                error_log('FC Topic Sync Error: ' . $e->getMessage());
            }
        }

I do not get any errors in the PHP error log but nothing changes in the topics of the post. :(

Any help or suggestions or links to some docs on coding these types of options for a post is appreciated.

Jamie Robe

OK, Gemini actually helped me out with #2 above. It suggested I change "topics" to "terms" in the code above.

            if (method_exists($feed, 'terms')) {
                $feed->terms()->sync([$topicId]);
            }

This updates to the post topic :)'

Note that I am currently passing the topicId into the function, not the name of the topic. You can look the id up in the wp_fcom terms DB table.

Still not clear on #1, disabling the discussion comments for a post.

Thomas Oates

Jamie RobeΒ I haven't tested this at all but this might work:

$feed->meta['comments_disabled'] = 'yes';

Put before saving the feed.

Jamie Robe

Thomas OatesΒ Thanks but it did not work. I have tried so many things I have lost track. Not a game changer for me as I can set them manually after the posts are automatically created and then they remain set to yes. But I hate not knowing how to do it properly.