Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/12/2014, 10:27
Avatar de madman_18
madman_18
 
Fecha de Ingreso: agosto-2010
Mensajes: 792
Antigüedad: 13 años, 8 meses
Puntos: 14
Exclamación Usar have_posts() + SHORTINIT en ajax

Hola a todos!

Estoy un poco atascado en el uso de ajax+shortinit(para hacerlo más rápido).

Os pongo mi código:

En mi ajax.php tengo lo siguiente:

Código PHP:
Ver original
  1. define('SHORTINIT',true);
  2. define('AUTH_COOKIE',true);
  3. define('LOGGED_IN_COOKIE',true);
  4. require_once ('../../../../wp-load.php');
  5. require_once ('../../../../wp-includes/taxonomy.php');
  6. require_once ('../../../../wp-includes/meta.php');
  7. require_once ('../../../../wp-includes/post.php');
  8. require_once ('../../../../wp-includes/user.php');
  9. require_once ('../../../../wp-includes/pluggable.php');
  10. require_once ('../../../../wp-includes/capabilities.php');
  11. require_once ('../../../../wp-includes/comment.php');
  12. require_once ('../../../../wp-includes/formatting.php');
  13. require_once ('../../../../wp-includes/query.php');
  14. require_once ('ajax-funciones.php');
  15. if ((!isset($_POST["action"])) || ($_POST["action"]!=="") || (!is_numeric($_POST["action"]))) {
  16.     switch($_POST["action"]){
  17.         case 'buscaPost':
  18.             if ((isset($_POST["miPost"])) || (isset($_POST["slug"]))){
  19.                 sacaPost($_POST["miPost"],$_POST["slug"]);             
  20.             }else{
  21.                 echo "No hay variables";
  22.             }
  23.             break;
  24.     }
  25. }
  26. die();

Y en mi ajax-funciones.php tengo lo siguiente (la función sacaPost)
Código PHP:
Ver original
  1. function sacaPost($idPost,$slugPost){  
  2.     // Buscamos el contenido del post
  3.     $query = array("cat"=>$idPost);
  4.     $objetoDatos = new WP_Query($query);   
  5.     if ($objetoDatos->have_posts()){
  6.         while($objetoDatos->have_posts()){
  7.             $objetoDatos->the_post();
  8.             echo get_the_content();            
  9.         }
  10.         wp_reset_postdata();
  11.     }else{
  12.         echo "any";
  13.     }  
  14. }

He probado ya bastantes cosas pero siempre me devuelve "any" al hacer la llamada. La llamada la hago al hacer un click en una lista que tengo y la hago de la siguiente forma:
Código Javascript:
Ver original
  1. jQuery.post(MyAjax.url,{action: 'buscaPost',miPost: miID,slug: miSlug},function(response){
  2.             jQuery("#contenido-conoceDerecha").html(response);
  3.             jQuery("#contenido-conoceDerecha").show('fade');           
  4.         });

Como véis la llamada es simple pero no se por qué el have_posts() siempre me está sacando como false, como si no existiese ninguna entrada cuando sí lo hay.

¿Qué puede ser el fallo?? Gracias!
__________________
"Si consigues ser algo más que un hombre, si te entregas a un ideal, te convertes en algo muy diferente"