WP term_exists and wp_insert_term issue











up vote
0
down vote

favorite












Maybe I'm trying to reinvent the wheel, but my issue is that I need to check do custom taxonomy term exists, if not than insert this term and afterwards grab term_taxonomy_id of inserted term and assign it to variable. This variable is later used to assign terms for post which will be inserted programmatically. I need to complete all these tasks in one function, because all this is run through wp_cron. At the moment I have tried to run this script from functions.php and from plugin as well – result is the same terms are added, but too late to grab term_taxonomy_id. If I run this function when terms are already in database than of course everything works...



for ( $i = 0; $i < $count; $i++ ) {
if ( empty( term_exists( $fast_new_categories[ $i ], 'fast-news-categories' ) ) ) {
$x = $fast_new_categories[ $i ];
add_action(
'init',
function () use ( &$x ) {
wp_insert_term( $x, 'fast-news-categories', array( 'parent' => 0 ) );
}
);
}
$fast_news_term = term_exists( $fast_new_categories[ $i ], 'fast-news-categories', 0 );
}


My wp_cron task sequence is:




  • I collect post data from API;

  • Than I iterate through it, and first
    thing I’m trying to do is to check for custom taxonomy term data;

  • Than I make array for post; Than I insert posts;

  • Than I assign terms of custom taxonomy for those just inserted posts – here I need this term_taxonomy_id from term check before.


I understand that I can do term_exists() from functions.php but I can not run wp_insert_term() from it directly, that is why I use add_action with init hook which runs much later.



Can somebody help me to resolve this issue?










share|improve this question


























    up vote
    0
    down vote

    favorite












    Maybe I'm trying to reinvent the wheel, but my issue is that I need to check do custom taxonomy term exists, if not than insert this term and afterwards grab term_taxonomy_id of inserted term and assign it to variable. This variable is later used to assign terms for post which will be inserted programmatically. I need to complete all these tasks in one function, because all this is run through wp_cron. At the moment I have tried to run this script from functions.php and from plugin as well – result is the same terms are added, but too late to grab term_taxonomy_id. If I run this function when terms are already in database than of course everything works...



    for ( $i = 0; $i < $count; $i++ ) {
    if ( empty( term_exists( $fast_new_categories[ $i ], 'fast-news-categories' ) ) ) {
    $x = $fast_new_categories[ $i ];
    add_action(
    'init',
    function () use ( &$x ) {
    wp_insert_term( $x, 'fast-news-categories', array( 'parent' => 0 ) );
    }
    );
    }
    $fast_news_term = term_exists( $fast_new_categories[ $i ], 'fast-news-categories', 0 );
    }


    My wp_cron task sequence is:




    • I collect post data from API;

    • Than I iterate through it, and first
      thing I’m trying to do is to check for custom taxonomy term data;

    • Than I make array for post; Than I insert posts;

    • Than I assign terms of custom taxonomy for those just inserted posts – here I need this term_taxonomy_id from term check before.


    I understand that I can do term_exists() from functions.php but I can not run wp_insert_term() from it directly, that is why I use add_action with init hook which runs much later.



    Can somebody help me to resolve this issue?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Maybe I'm trying to reinvent the wheel, but my issue is that I need to check do custom taxonomy term exists, if not than insert this term and afterwards grab term_taxonomy_id of inserted term and assign it to variable. This variable is later used to assign terms for post which will be inserted programmatically. I need to complete all these tasks in one function, because all this is run through wp_cron. At the moment I have tried to run this script from functions.php and from plugin as well – result is the same terms are added, but too late to grab term_taxonomy_id. If I run this function when terms are already in database than of course everything works...



      for ( $i = 0; $i < $count; $i++ ) {
      if ( empty( term_exists( $fast_new_categories[ $i ], 'fast-news-categories' ) ) ) {
      $x = $fast_new_categories[ $i ];
      add_action(
      'init',
      function () use ( &$x ) {
      wp_insert_term( $x, 'fast-news-categories', array( 'parent' => 0 ) );
      }
      );
      }
      $fast_news_term = term_exists( $fast_new_categories[ $i ], 'fast-news-categories', 0 );
      }


      My wp_cron task sequence is:




      • I collect post data from API;

      • Than I iterate through it, and first
        thing I’m trying to do is to check for custom taxonomy term data;

      • Than I make array for post; Than I insert posts;

      • Than I assign terms of custom taxonomy for those just inserted posts – here I need this term_taxonomy_id from term check before.


      I understand that I can do term_exists() from functions.php but I can not run wp_insert_term() from it directly, that is why I use add_action with init hook which runs much later.



      Can somebody help me to resolve this issue?










      share|improve this question













      Maybe I'm trying to reinvent the wheel, but my issue is that I need to check do custom taxonomy term exists, if not than insert this term and afterwards grab term_taxonomy_id of inserted term and assign it to variable. This variable is later used to assign terms for post which will be inserted programmatically. I need to complete all these tasks in one function, because all this is run through wp_cron. At the moment I have tried to run this script from functions.php and from plugin as well – result is the same terms are added, but too late to grab term_taxonomy_id. If I run this function when terms are already in database than of course everything works...



      for ( $i = 0; $i < $count; $i++ ) {
      if ( empty( term_exists( $fast_new_categories[ $i ], 'fast-news-categories' ) ) ) {
      $x = $fast_new_categories[ $i ];
      add_action(
      'init',
      function () use ( &$x ) {
      wp_insert_term( $x, 'fast-news-categories', array( 'parent' => 0 ) );
      }
      );
      }
      $fast_news_term = term_exists( $fast_new_categories[ $i ], 'fast-news-categories', 0 );
      }


      My wp_cron task sequence is:




      • I collect post data from API;

      • Than I iterate through it, and first
        thing I’m trying to do is to check for custom taxonomy term data;

      • Than I make array for post; Than I insert posts;

      • Than I assign terms of custom taxonomy for those just inserted posts – here I need this term_taxonomy_id from term check before.


      I understand that I can do term_exists() from functions.php but I can not run wp_insert_term() from it directly, that is why I use add_action with init hook which runs much later.



      Can somebody help me to resolve this issue?







      php wordpress wordpress-theming






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 22:06









      Mārtiņš Līgotnis

      53




      53





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383312%2fwp-term-exists-and-wp-insert-term-issue%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383312%2fwp-term-exists-and-wp-insert-term-issue%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)