1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

DataWriter tags

Discussion in 'XenForo Development Discussions' started by LPH, Dec 30, 2015.

  1. LPH

    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:

    $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.

    array (size=1)
      0 =>
          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.

    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: Dec 30, 2015
  2. Jeremy P

    Jeremy P Well-Known Member

    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).
  3. Jeremy P

    Jeremy P Well-Known Member

    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:

            // ... 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 (
            ) {
    /** @var XenForo_Model_Tag $tagModel */
    $tagModel $this->getModelFromCache('XenForo_Model_Tag');
    $tagger $tagModel->getTagger('thread');

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


    // ...


    $thread $threadWriter->getMergedData();

            if (
    $tagger->setContent($thread['thread_id'], true)

    // ...
    Last edited: Dec 31, 2015
    Robust and LPH like this.
  4. LPH

    LPH Well-Known Member

    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 !

    Jeremy P likes this.
  5. Jeremy P

    Jeremy P Well-Known Member

    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.

    Haha, always something simple. I'm glad to hear you got it working! :D
    Robust and LPH like this.

Share This Page