$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>posts - wp_query with ajax|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

posts - wp_query with ajax

matteradmin8PV0评论

I am trying to filter posts by year using WP_QUERY with AJAX And the current builder i am using is DIVI(for front end).

Result

Everything loads fine and i am facing one final roadblock that is when I filter the posts using the select dropdown, the result that it throws back include the html tags.

My results can be found in this attachment.

Expected Results

I want to load the posts content without this html tags.

What I have tried

I have tried using

$content = apply_filters( 'the_content' , wp_strip_all_tags(get_the_content())); 

but it still spits out the html tag.

This is my full code for this query

functions.php

// to connect to ajax
function asb_blog_clone()
{
  if (is_page(7526)) {
    wp_enqueue_script('asb_blog_clone.js' ,get_template_directory_uri() . '/js/asb_blog_clone.js' , array('jquery') , false);
   }
}
add_action('wp_enqueue_scripts' , asb_blog_clone);

// to load the result from ajax
function filter_post_asb_clone()
{
    // ob_start();
    $args = array(
        'year' => $_POST['year'] ,
        'cat' => array(42 , 43)
    );
    $query = new WP_Query($args);
    if ( $query -> have_posts()) :
?>
    <div class="container">
        <ul class="display-posts-listing">
            <?php while($query -> have_posts()) : $query -> the_post(); ?>
            <li class="listing-item">
                <h4><?php the_title();?></h4>
                <?php
                    $content = apply_filters( 'the_content' , get_the_content());
                    echo $content;
                ?>
                <?php the_post_thumbnail();?>
            </li>
            <?php endwhile ; ?>
            <?php wp_reset_postdata();?>
        </ul>
    </div>
<?php
    endif;
    // echo ob_get_clean();
    return $query;
    exit;
}
add_action('wp_ajax_year_selector', 'filter_post_asb_clone');
add_action('wp_ajax_nopriv_year_selector', 'filter_post_asb_clone');

asb_blog_clone.js [To send Ajax Request]

jQuery(document).ready(function($)
{
    // add css for select
    const start = 2017;
    const year = $('#year')
    displayYear(start)
    setValueToOption()
    year.change(function(){
        let $year = $('#year option:selected').text()
        $.ajax({
            url:'.php' , //#endregion
            type : 'POST' , //#endregion
            data : 'action=year_selector&year=' + $year , //#endregion
            success : function (results) {
                // $('.load-result').html(results)
                $('.display-posts-listing').children().empty();
                $(results).addClass('.listing-item')
                $('.display-posts-listing').append(results)
            }
        })
    })
    year.css({
        'background-color' : '#0D3A7C' ,
        'color' : '#fff',
        'width' : '25%'
    })
    year.mouseenter(function(){
        year.css('background-color' , '#AA1E46')
    })
    year.mouseleave(function(){
        year.css('background-color' , '#0D3A7C')
    })
})

Kindly advise on this please.

Much appreciated and thank you for your time.

I am trying to filter posts by year using WP_QUERY with AJAX And the current builder i am using is DIVI(for front end).

Result

Everything loads fine and i am facing one final roadblock that is when I filter the posts using the select dropdown, the result that it throws back include the html tags.

My results can be found in this attachment.

Expected Results

I want to load the posts content without this html tags.

What I have tried

I have tried using

$content = apply_filters( 'the_content' , wp_strip_all_tags(get_the_content())); 

but it still spits out the html tag.

This is my full code for this query

functions.php

// to connect to ajax
function asb_blog_clone()
{
  if (is_page(7526)) {
    wp_enqueue_script('asb_blog_clone.js' ,get_template_directory_uri() . '/js/asb_blog_clone.js' , array('jquery') , false);
   }
}
add_action('wp_enqueue_scripts' , asb_blog_clone);

// to load the result from ajax
function filter_post_asb_clone()
{
    // ob_start();
    $args = array(
        'year' => $_POST['year'] ,
        'cat' => array(42 , 43)
    );
    $query = new WP_Query($args);
    if ( $query -> have_posts()) :
?>
    <div class="container">
        <ul class="display-posts-listing">
            <?php while($query -> have_posts()) : $query -> the_post(); ?>
            <li class="listing-item">
                <h4><?php the_title();?></h4>
                <?php
                    $content = apply_filters( 'the_content' , get_the_content());
                    echo $content;
                ?>
                <?php the_post_thumbnail();?>
            </li>
            <?php endwhile ; ?>
            <?php wp_reset_postdata();?>
        </ul>
    </div>
<?php
    endif;
    // echo ob_get_clean();
    return $query;
    exit;
}
add_action('wp_ajax_year_selector', 'filter_post_asb_clone');
add_action('wp_ajax_nopriv_year_selector', 'filter_post_asb_clone');

asb_blog_clone.js [To send Ajax Request]

jQuery(document).ready(function($)
{
    // add css for select
    const start = 2017;
    const year = $('#year')
    displayYear(start)
    setValueToOption()
    year.change(function(){
        let $year = $('#year option:selected').text()
        $.ajax({
            url:'http://uat.asb.edu.my/wp-admin/admin-ajax.php' , //#endregion
            type : 'POST' , //#endregion
            data : 'action=year_selector&year=' + $year , //#endregion
            success : function (results) {
                // $('.load-result').html(results)
                $('.display-posts-listing').children().empty();
                $(results).addClass('.listing-item')
                $('.display-posts-listing').append(results)
            }
        })
    })
    year.css({
        'background-color' : '#0D3A7C' ,
        'color' : '#fff',
        'width' : '25%'
    })
    year.mouseenter(function(){
        year.css('background-color' , '#AA1E46')
    })
    year.mouseleave(function(){
        year.css('background-color' , '#0D3A7C')
    })
})

Kindly advise on this please.

Much appreciated and thank you for your time.

Share Improve this question asked Feb 14, 2019 at 3:36 deshdesh 1372 silver badges10 bronze badges 2
  • you're calling wp_strip_all_tags on the content, then passing that result to apply_filters, which is the thing that's adding the html tags. you're trying to remove the tags before they've been added. – Milo Commented Feb 14, 2019 at 3:45
  • Hi, so remove it and just echo get_the_content() right? – desh Commented Feb 14, 2019 at 4:10
Add a comment  | 

1 Answer 1

Reset to default 1

So, i found a work around for this . I just display the excerpt() and then I link them to the permalink() of this post.

// to load the result from ajax

function filter_post_asb_clone()
{
    // ob_start();
    $args = array(
        'year' => $_POST['year'] ,
        'cat' => array(42 , 43)
    );
    $query = new WP_Query($args);
    if ( $query -> have_posts()) :
?>
    <div class="container">
        <ul class="display-posts-listing">
            <?php while($query -> have_posts()) : $query -> the_post(); ?>
            <a href="<?php echo the_permalink(); ?>">
                <li class="listing-item">
                <h4><?php the_title();?></h4>
                <?php
                    echo the_excerpt();
                ?>
                <?php the_post_thumbnail();?>
               </li>
            </a>
            <?php endwhile ; ?>
            <?php wp_reset_postdata();?>
        </ul>
    </div>
<?php
    endif;
    exit;
}

Thank you very much

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far