0

I'm bulding a new web site and I need use ajax to search and return some pages/post. I don't know the problem but my ajax need 2s or 3s to return the HTML structure with the page and some photos (this photos are for a slider).

In my ajax I return the HTML structure but it's a simple HTML (some divs, photos and the javascript to build the slider).

What can be the problem?

My ajax code is:

if ((!isset($_POST["miPost"])) || (!isset($_POST["slug"]))){
        echo "<p>Error.</p>";
        die();
    }
    //Buscamos los datos del post   
    $querySlider = array('category_name'=>'slider-'.$_POST["slug"],'order'=>"ASC");
    $objetoSlider = new WP_Query($querySlider); 
    if ($objetoSlider->have_posts()){
        ?>
        <div class="sliderSeccion-conoce">
            <div class="slider-wrapper theme-default">
                <div id="slider-conoce" class="nivoSlider">
                <?php
                    while($objetoSlider->have_posts()){
                        $objetoSlider->the_post();
                        if (has_post_thumbnail()){
                            the_post_thumbnail();
                        }
                    }   
                ?>                  
                </div>
            </div>
        </div>
        <?php   
        wp_reset_postdata();
    }
    ?>
    <?php 
        $queryDatos = array("cat"=>$_POST["miPost"]);
        $objetoDatos = new WP_Query($queryDatos);
        if ($objetoDatos->have_posts()){
        ?>
            <div class="contenido-conoce">
                <?php
                    while($objetoDatos->have_posts()){
                        $objetoDatos->the_post();                       
                        ?>
                        <div class="tituloContenido-conoce"><p><?php the_title();?></p></div>
                        <div class="textoContenido-conoce"><?php the_content();?></div>
                        <?php
                    }                   
                    if ($_POST["slug"]=="origenes"){
                        ?>
                        <a class="enlaceContenido-conoce" href="mysite" target="_blank">M&aacute;s informaci&oacute;n.</a>
                        <?php   
                    }
                ?>
            </div>
            <?php       
            wp_reset_postdata();
        }
    ?>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#slider-conoce").nivoSlider({
                directionNav:true,
                effect:'fade',
                pauseTime: 4000,
                animSpeed:1000,
                slices:1
            });
            $("#contenido-conoceDerecha").show("fade");
        });
    </script>
    <?php   
    die();

Thanks!

Edit:

I try to insert some code like this (like the link who put in a comments):

define("SHORTINIT",0);
require 'wp-load.php';
require(ABSPATH.WPINC.'/formatting.php');
require(ABSPATH.WPINC.'/meta.php');
require(ABSPATH.WPINC.'/post.php');

But always give me some errors:

Notice: Constant SHORTINIT already defined.....

Constant ABSPATH already defined

This code has been write in the functions file or what can be construct a file to put all my ajax's functions (called to him, etc...)?

  • It is difficult to know seeing you code. How many posts are returned (slider + posts)? How big are the images? Aside recommendation: don't use &aacute;, &oacute; and so on. Instead use á, ó, ... and encode your script and HTML in UTF-8. – cybmeta Dec 16 '14 at 8:27
  • The max post to return are 4 post in total (3 photos and 1 post with text), in other cases are 2 or 3 @cybmeta – Marcos Dec 16 '14 at 8:41
  • 1
    I believe this issue is similar to this one here wordpress.stackexchange.com/questions/41808/… – Karun Dec 16 '14 at 9:18
  • @Karun I think is not the same issue. It is true that you can use the SHORTINIT option in ajax calls made through direct PHP script calls; but it doesn't explain why the code of Marcos takes 2 - 3 seconds. Think about it. In a context with high number of ajax requests, using SHORTINIT can save a lot of server resources and reduce response time but I think this is not the case and the use or not of SHORTINIT doesn't explain why this ajax action is so slow. – cybmeta Dec 16 '14 at 10:36
  • It is impossible to guess what is slow either. There is nothing inherently more slow in Ajax endpoint that any other WordPress core load. – Rarst Dec 16 '14 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.