DataWriter tags

LPH

Well-known member
Trying to get tags from WordPress post to be written to a XenForo thread. An array of tags is grabbed using:

PHP:
$tags = get_the_tags($post->ID);

A Zend_Dubug::dump($tags) shows the array such that $tags['name'] is the name of the each tag for the post.

Code:
array (size=1)
  0 =>
    object(WP_Term)[451]
      public 'term_id' => int 8
      public 'name' => string 'XenWord sales' (length=13)
      public 'slug' => string 'xenword-sales' (length=13)
      public 'term_group' => int 0
      public 'term_taxonomy_id' => int 8
      public 'taxonomy' => string 'post_tag' (length=8)
      public 'description' => string '' (length=0)
      public 'parent' => int 0
      public 'count' => int 3
      public 'filter' => string 'raw' (length=3)

Something as simple as doing a foreach and trying to write $postWriter->set( 'tags', $tag['name'] ) doesn't work.

Update Inserted: This makes sense, too, since Zend::debug($tags[0]) gets to the object.

PHP:
foreach ($tags as $tag) {
    $postWriter->set( 'tags', $tag['name'] );
}

Error is Cannot use object of type WP_Term as array

Is the $postWriter->set proper or is there another way to write the tags to the thread?
 
Last edited:
The `$tags` variable is returning an array of objects (specifically of the class WP_Term). You can iterate over it with the foreach loop, but the iterated `$tag` is not an array itself.

`$tag['name']` only works for arrays. Try `$tag->name` instead, which is the correct way to access properties of an object.

Edit: To be clear, this probably still won't set the datawriter correctly as the value will be overwritten every time it loops. I haven't looked into how to properly set multiple tags in the datawriter, but the above is how you'd properly get the tag names (and where your error comes from).
 
See XenForo_ControllerPublic_Forum::actionAddThread() to see how tags are set. It's not as simple as setting them in the datawriter.

Here is an (untested) example of how I would do it:

PHP:
        // ... your code to create the thread

        // get tags and place all tag names into an array
        $rawTags = get_the_tags($post->ID);
        $tags    = [];
 
        foreach ($rawTags as $tag) {
            $tags[] = $tag->name
        }

        $tagger = null;

        // $forum is set via the ForumThreadPost helper
        if (
            $this->getModelFromCache('XenForo_Model_Thread')
                ->canEditTags(null, $forum)
        ) {
            /** @var XenForo_Model_Tag $tagModel */
            $tagModel = $this->getModelFromCache('XenForo_Model_Tag');
            $tagger = $tagModel->getTagger('thread');
            $tagger->setPermissionsFromContext($forum)
                ->setTags($tags);
            $threadWriter->mergeErrors($tagger->getErrors());
        }

        // ... possibly more code involved in thread creation

        $threadWriter->preSave();

        // ...

        $threadWriter->save();

        $thread = $threadWriter->getMergedData();

        if ($tagger)
        {
            $tagger->setContent($thread['thread_id'], true)
                ->save();
        }

        // ...
 
Last edited:
It's not as simple as setting them in the datawriter.

Thank you. I was just looking through the code in the ControllerPublic and saw using set was not correct as well as the use of tagger.

Your code is now modified to the point of adding tags; the code cannot use $this for creating the models. Actually, almost four hours ago but the tags were actually being transferred from WordPress to XenForo but they were not showing above the thread.

After eating dinner and relaxing, came back to the issue and looked in the admin panel admin.php?tags/ . Sure enough the tags were in the admin area and showed the tags were associated with the thread. Changed to the default theme to see what was happening but the tags were still not showing above the thread. Looked in admin and reverted templates. Ta Da -- TAGS !

(y)
 
Your code is now modified to the point of adding tags; the code cannot use $this for creating the models.
Ah right, that would only work inside of certain classes like controllers. I took most of the code from the core controller and didn't consider that.

Actually, almost four hours ago but the tags were actually being transferred from WordPress to XenForo but they were not showing above the thread.

After eating dinner and relaxing, came back to the issue and looked in the admin panel admin.php?tags/ . Sure enough the tags were in the admin area and showed the tags were associated with the thread. Changed to the default theme to see what was happening but the tags were still not showing above the thread. Looked in admin and reverted templates. Ta Da -- TAGS !

(y)
Haha, always something simple. I'm glad to hear you got it working! :D
 
Back
Top Bottom