• Resolved 8mikelx8

    (@8mikelx8)


    Ok, i created a plugin to process the text and create custom tags for it. Te problem is that when i call the have_posts() it doesn’t return me anything. i even tried the rewind_posts() before it but nothing changes. Here’s the code:

    <?php
    add_action( 'admin_menu', 'wp_autoTAG' );
    
    function wp_autoTAG() {
        add_options_page( 'Opciones WP autoTAG', 'Wp autoTAG', 'manage_options', 'wp_autoTAG', 'wp_autoTAG_options' );
    }
    
    function wp_autoTAG_options() {
        echo "function wp_autotag_options()";
        if (!current_user_can('manage_options'))
        {
            wp_die( __('Peque?±o padawan... debes utilizar la fuerza para entrar aquí-.') );
        }
    
        if(isset($_POST['gentags'])) {
          echo "isset(post['gentags'])";
            $limite = $_POST['relevancia'];
            $valuetoken = $_POST['apitoken'];
            $optionapi = get_option('apitoken','0');
            $arraytags = array();
            if(isset($_POST['apitoken'])){
              echo "isset(post['apitoken'])";
              if(!($optionapi === $valuetoken)){
                add_option('apitoken', $valuetoken);
                update_option('apitoken', $valuetoken);
                echo "option added, valor apitoquen guardat";
              }
            }
    
            //THE LOOP
            rewind_posts();
            if(have_posts()){
              $contador = 0;
              echo "have_posts";
              while(have_posts()){
                echo "bucle $contador";
                $contador += 1;
                if($contador > 4){
                  break;
                }
                the_post();
    
                $post_id = the_ID();
                $content = get_the_content();
    
                //FUNCIóN DE ANALISIS DE TEXTO
    
                //ANALISIS DE ENTIDADES
                if(isset($_POST['personas']) || isset($_POST['lugares']) || isset($_POST['organizaciones']) || isset($_POST['otros'])){
                  echo "entidades analizadas";
          				$url = 'https://api.monkeylearn.com/v2/extractors/ex_Kc8uzhSi/extract/';
          				$data = array('text_list' => array($content));
          				$options = array(
          				    'http' => array(
          				        'header'  => "Content-type: application/json\r\n".
          				            "Authorization:token $valuetoken\r\n",
          				        'method'  => 'POST',
          				        'content' => json_encode($data),
          				    ),
          				);
          				$context  = stream_context_create($options);
          				$result = file_get_contents($url, false, $context);
    
          				//var_dump($result);
    
          				$jsonIterator = new RecursiveIteratorIterator(
          				    new RecursiveArrayIterator(json_decode($result, TRUE)),
          				    RecursiveIteratorIterator::SELF_FIRST);
                  $push = false;
          				foreach ($jsonIterator as $key => $val) {
          				    if(is_array($val)) {
          				    }
                      else {
                        if(isset($_POST['personas'])){
                          if($key === 'tag' && $val ==='PERS'){
                            $push = true;
                          }
                        }
                        if(isset($_POST['lugares'])){
                          if($key === 'tag' && $val ==='LUG'){
                            $push = true;
                          }
                        }
                        if(isset($_POST['organizaciones'])){
                          if($key === 'tag' && $val ==='ORG'){
                            $push = true;
                          }
                        }
                        if(isset($_POST['otros'])){
                          if($key === 'tag' && $val ==='OTROS'){
                            $push = true;
                          }
                        }
          			        if($key === 'entity' && $push == true){
          			          $entity = $val;
          			          array_push($arraytags, $entity);
                          $push = false;
          			        }
          				    }
          				}
                }
                if(isset($_POST['keywords'])){
                  echo "analisis de palabras clave";
          				//ANALISIS DE KEYWORDS CON RELEVANCIA
    
                  $limite = $_POST['relevancia'];
          				$url = 'https://api.monkeylearn.com/v2/extractors/ex_eV2dppYE/extract/';
          				$data = array('text_list' => array($content));
          				$options = array(
          				    'http' => array(
          				        'header'  => "Content-type: application/json\r\n".
          				            "Authorization:token $valuetoken\r\n",
          				        'method'  => 'POST',
          				        'content' => json_encode($data),
          				    ),
          				);
          				$context  = stream_context_create($options);
          				$result2 = file_get_contents($url, false, $context);
          				$jsonIterator = new RecursiveIteratorIterator(
          				    new RecursiveArrayIterator(json_decode($result2, TRUE)),
          				    RecursiveIteratorIterator::SELF_FIRST);
    
          				foreach ($jsonIterator as $key => $val) {
          				    if(is_array($val)) {
          				    }
          				    else {
          				        //COMPROBAR RELEVANCIA POR ENCIMA DEL LIMITE ESTABLECIDO
    
          				        if($key ==='relevance'){
          				          if(floatval($val) > floatval($limite)){
          				            $push = true;
          				          }
          				          else{
          				            $push = false;
          				          }
          				        }
          				        //A?ADIR KEYWORDS RELEVANTES AL ARRAY
          				        if($key ==='keyword'){
          				          if($push == true){
          				            array_push($arraytags, $val);
          				          }
          				        }
          				    }
          				}
                } // final de if(isset(post[keywords]))
                wp_set_post_tags( $post_id, $arraytags);
              }// final while(have posts())
            }//FINAL THE LOOP
            else{
              echo "no_have_posts";
            }
    
            ?>
                <div class="updated">
                    <p>
                        <strong>
                            <?php _e('TAGS generados'); ?>
                        </strong>
                    </p>
                </div>
            <?php
    
          }
        echo '<div class="wrap">';
    
        echo "<h2>" . __( 'auto TAG') . "</h2>";
        $optionapi = get_option('apitoken','0');
        ?>
        <form name="form1" method="post" action="">
          <fieldset style="border:solid black 1px; padding: 15px;">
          <p style="margin:0 1em 0 2em;">
            <h3><span style="margin-right:1em;"> Clave de API</span></h3>
              <input type="text" name="apitoken" size="42" value="<?php echo $optionapi ?>"/></br>
               Si no dispone de Clave puede conseguir una <a href="https://app.monkeylearn.com/accounts/register/">aquí</a>
          </p>
        </fieldset>
          <fieldset style="border:solid black 1px; padding: 15px;">
            <p style="margin:0 1em 0 2em;">
              <h3><span style="margin-right:1em;"> TAGS por palabra clave</span> <input type="checkbox" name="keywords"/></h3>
                <?php _e("Relevancia mínima: "); ?>
                <input type="number" name="relevancia" min="0.1" max="1" step="0.1" value="0.5"/>
            </p>
          </fieldset>
          <fieldset style="border:solid black 1px; padding: 15px;">
            <p style="margin:0 1em 0 2em;">
              <h3> TAGS por entidades</h3>
              <ul style="width:100%; list-style:none; margin:0; padding:0; float:left; text-align: left;">
                <li style="float:left; width:25%; margin:0; paddind:0; list-style: none;">
                  <input type="checkbox" name="personas"/>
                  <?php _e("PERSONAS"); ?>
                </li>
                <li style="float:left; width:25%; margin:0; paddind:0; list-style: none;">
                  <input type="checkbox" name="lugares"/>
                  <?php _e("LUGARES"); ?>
                </li>
                <li style="float:left; width:25%; margin:0; paddind:0; list-style: none;">
                  <input type="checkbox" name="organizaciones"/>
                  <?php _e("ORGANIZACIONES"); ?>
                </li>
                <li style="float:left; width:25%; margin:0; paddind:0; list-style: none;">
                  <input type="checkbox" name="otros"/>
                  <?php _e("OTROS"); ?>
                </li>
            </ul>
            </p>
          </fieldset>
          <p class="submit">
              <input type="submit" name="gentags" class="button-primary" value="Generar TAGS" />
          </p>
        </form>
    </div>
    
    <?php
    var_dump($arraytags);
    }
    
    ?>
Viewing 1 replies (of 1 total)
  • Thread Starter 8mikelx8

    (@8mikelx8)

    The function have_posts() seems not to work inside wp admin. i solved it by using get_posts().

Viewing 1 replies (of 1 total)
  • The topic ‘Have_posts() does not find posts in my plugin’ is closed to new replies.