admin管理员组

文章数量:1025235

I'm trying to create a simple Ajax posts filter, It's working with date and comment but not working with the top views or top likes.

So, how can i make the isset( $_POST['top-views'] and isset( $_POST['top-likes'] work when I select it.

I'm highly appreciated your help.

form

<select name="misha_order_by" id="misha_order_by">
    <option name="top-views" value="top-views">Top Views</option>
    <option name="top-likes" value="top-likes">Top Likes</option>
    <option value="comment_count-DESC">Comments ↓</option>
    <option value="comment_count-ASC">Comments ↑</option>
    <option value="date-DESC">Date ↓</option>
    <option value="date-ASC">Date ↑</option>
</select>

Ajax function.

add_action('wp_ajax_mishainsightsfilter', 'misha_insights_filter_function'); 
add_action('wp_ajax_nopriv_mishainsightsfilter', 'misha_insights_filter_function');

function misha_insights_filter_function(){

global $current_user;
get_currentuserinfo();

    // example: date-ASC 
    $order = explode( '-', $_POST['misha_order_by'] );

    $params = array(
        'posts_per_page' => $_POST['misha_number_of_results'], // when set to -1, it shows all posts
        'author' => $current_user->ID,
        'orderby' => $order[0], // example: date
        'order' => $order[1] // example: ASC
    );

      if( isset( $_POST['top-views'] ) )
            $params = array(
                    'author' => $current_user->ID,
                    'meta_key' => 'post_views_count', 
                    'orderby' => 'meta_value_num',
                    'order' => 'DESC'
       );

      if( isset( $_POST['top-likes'] ) )
            $params = array(
                    'author' => $current_user->ID,
                    'meta_key' => '_post_like_count', 
                    'orderby' => 'meta_value_num',
                    'order' => 'DESC'
       );


    query_posts( $params );

    global $wp_query;

    if( have_posts() ) :

        ob_start(); // start buffering because we do not need to print the posts now

        while( have_posts() ): the_post();

            the_title();

        endwhile;

        $posts_html = ob_get_contents(); // we pass the posts to variable
        ob_end_clean(); // clear the buffer
    else:
        $posts_html = '<p>Nothing found for your criteria.</p>';
    endif;

    // no wp_reset_query() required

    echo json_encode( array(
        'posts' => json_encode( $wp_query->query_vars ),
        'max_page' => $wp_query->max_num_pages,
        'found_posts' => $wp_query->found_posts,
        'content' => $posts_html
    ) );

    die();
}

I'm trying to create a simple Ajax posts filter, It's working with date and comment but not working with the top views or top likes.

So, how can i make the isset( $_POST['top-views'] and isset( $_POST['top-likes'] work when I select it.

I'm highly appreciated your help.

form

<select name="misha_order_by" id="misha_order_by">
    <option name="top-views" value="top-views">Top Views</option>
    <option name="top-likes" value="top-likes">Top Likes</option>
    <option value="comment_count-DESC">Comments ↓</option>
    <option value="comment_count-ASC">Comments ↑</option>
    <option value="date-DESC">Date ↓</option>
    <option value="date-ASC">Date ↑</option>
</select>

Ajax function.

add_action('wp_ajax_mishainsightsfilter', 'misha_insights_filter_function'); 
add_action('wp_ajax_nopriv_mishainsightsfilter', 'misha_insights_filter_function');

function misha_insights_filter_function(){

global $current_user;
get_currentuserinfo();

    // example: date-ASC 
    $order = explode( '-', $_POST['misha_order_by'] );

    $params = array(
        'posts_per_page' => $_POST['misha_number_of_results'], // when set to -1, it shows all posts
        'author' => $current_user->ID,
        'orderby' => $order[0], // example: date
        'order' => $order[1] // example: ASC
    );

      if( isset( $_POST['top-views'] ) )
            $params = array(
                    'author' => $current_user->ID,
                    'meta_key' => 'post_views_count', 
                    'orderby' => 'meta_value_num',
                    'order' => 'DESC'
       );

      if( isset( $_POST['top-likes'] ) )
            $params = array(
                    'author' => $current_user->ID,
                    'meta_key' => '_post_like_count', 
                    'orderby' => 'meta_value_num',
                    'order' => 'DESC'
       );


    query_posts( $params );

    global $wp_query;

    if( have_posts() ) :

        ob_start(); // start buffering because we do not need to print the posts now

        while( have_posts() ): the_post();

            the_title();

        endwhile;

        $posts_html = ob_get_contents(); // we pass the posts to variable
        ob_end_clean(); // clear the buffer
    else:
        $posts_html = '<p>Nothing found for your criteria.</p>';
    endif;

    // no wp_reset_query() required

    echo json_encode( array(
        'posts' => json_encode( $wp_query->query_vars ),
        'max_page' => $wp_query->max_num_pages,
        'found_posts' => $wp_query->found_posts,
        'content' => $posts_html
    ) );

    die();
}
Share Improve this question asked Apr 11, 2019 at 7:22 Adham MohamedAdham Mohamed 1853 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I haven't tested the code, but this should help.

option elements do not have a name attribute:

<option name="top-views" value="top-views">Top Views</option>
<option name="top-likes" value="top-likes">Top Likes</option>

And therefore $_POST['top-views'] for example, is not going to be set, or that the value is not the top-views in the above option.

Use the proper option markup

Based on the following markup:

<option value="comment_count-DESC">Comments ↓</option>

Your custom option should be:

<option value="{key}-{order}">{label}</option>

So (to sort in DESCending order):

<option value="top_views-DESC">Top Views</option>
<option value="top_likes-DESC">Top Likes</option>

Note that the {key} should not have a dash (-).

Parsing the sorting {key} and {order}

In the misha_insights_filter_function() function, the sorting {key} (i.e. the orderby parameter) and {order} are parsed like so:

$order = explode( '-', $_POST['misha_order_by'] );

So for example, if "Top Views" was selected, and that the option value was top_views-DESC, then $order[0] would be top_views and $order[1] would be DESC.

And you can then customize the $param like so:

if ( 'top_views' === $order[0] ) {
  $params['meta_key'] = 'post_views_count';
  $params['orderby'] = 'meta_value_num';
}

Similarly, if "Top Likes" was selected, and that the option value was top_likes-DESC, you can just copy-paste the above if block and replace the relevant values:

if ( 'top_likes' === $order[0] ) {
  $params['meta_key'] = '_post_like_count';
  $params['orderby'] = 'meta_value_num';
}

I'm trying to create a simple Ajax posts filter, It's working with date and comment but not working with the top views or top likes.

So, how can i make the isset( $_POST['top-views'] and isset( $_POST['top-likes'] work when I select it.

I'm highly appreciated your help.

form

<select name="misha_order_by" id="misha_order_by">
    <option name="top-views" value="top-views">Top Views</option>
    <option name="top-likes" value="top-likes">Top Likes</option>
    <option value="comment_count-DESC">Comments ↓</option>
    <option value="comment_count-ASC">Comments ↑</option>
    <option value="date-DESC">Date ↓</option>
    <option value="date-ASC">Date ↑</option>
</select>

Ajax function.

add_action('wp_ajax_mishainsightsfilter', 'misha_insights_filter_function'); 
add_action('wp_ajax_nopriv_mishainsightsfilter', 'misha_insights_filter_function');

function misha_insights_filter_function(){

global $current_user;
get_currentuserinfo();

    // example: date-ASC 
    $order = explode( '-', $_POST['misha_order_by'] );

    $params = array(
        'posts_per_page' => $_POST['misha_number_of_results'], // when set to -1, it shows all posts
        'author' => $current_user->ID,
        'orderby' => $order[0], // example: date
        'order' => $order[1] // example: ASC
    );

      if( isset( $_POST['top-views'] ) )
            $params = array(
                    'author' => $current_user->ID,
                    'meta_key' => 'post_views_count', 
                    'orderby' => 'meta_value_num',
                    'order' => 'DESC'
       );

      if( isset( $_POST['top-likes'] ) )
            $params = array(
                    'author' => $current_user->ID,
                    'meta_key' => '_post_like_count', 
                    'orderby' => 'meta_value_num',
                    'order' => 'DESC'
       );


    query_posts( $params );

    global $wp_query;

    if( have_posts() ) :

        ob_start(); // start buffering because we do not need to print the posts now

        while( have_posts() ): the_post();

            the_title();

        endwhile;

        $posts_html = ob_get_contents(); // we pass the posts to variable
        ob_end_clean(); // clear the buffer
    else:
        $posts_html = '<p>Nothing found for your criteria.</p>';
    endif;

    // no wp_reset_query() required

    echo json_encode( array(
        'posts' => json_encode( $wp_query->query_vars ),
        'max_page' => $wp_query->max_num_pages,
        'found_posts' => $wp_query->found_posts,
        'content' => $posts_html
    ) );

    die();
}

I'm trying to create a simple Ajax posts filter, It's working with date and comment but not working with the top views or top likes.

So, how can i make the isset( $_POST['top-views'] and isset( $_POST['top-likes'] work when I select it.

I'm highly appreciated your help.

form

<select name="misha_order_by" id="misha_order_by">
    <option name="top-views" value="top-views">Top Views</option>
    <option name="top-likes" value="top-likes">Top Likes</option>
    <option value="comment_count-DESC">Comments ↓</option>
    <option value="comment_count-ASC">Comments ↑</option>
    <option value="date-DESC">Date ↓</option>
    <option value="date-ASC">Date ↑</option>
</select>

Ajax function.

add_action('wp_ajax_mishainsightsfilter', 'misha_insights_filter_function'); 
add_action('wp_ajax_nopriv_mishainsightsfilter', 'misha_insights_filter_function');

function misha_insights_filter_function(){

global $current_user;
get_currentuserinfo();

    // example: date-ASC 
    $order = explode( '-', $_POST['misha_order_by'] );

    $params = array(
        'posts_per_page' => $_POST['misha_number_of_results'], // when set to -1, it shows all posts
        'author' => $current_user->ID,
        'orderby' => $order[0], // example: date
        'order' => $order[1] // example: ASC
    );

      if( isset( $_POST['top-views'] ) )
            $params = array(
                    'author' => $current_user->ID,
                    'meta_key' => 'post_views_count', 
                    'orderby' => 'meta_value_num',
                    'order' => 'DESC'
       );

      if( isset( $_POST['top-likes'] ) )
            $params = array(
                    'author' => $current_user->ID,
                    'meta_key' => '_post_like_count', 
                    'orderby' => 'meta_value_num',
                    'order' => 'DESC'
       );


    query_posts( $params );

    global $wp_query;

    if( have_posts() ) :

        ob_start(); // start buffering because we do not need to print the posts now

        while( have_posts() ): the_post();

            the_title();

        endwhile;

        $posts_html = ob_get_contents(); // we pass the posts to variable
        ob_end_clean(); // clear the buffer
    else:
        $posts_html = '<p>Nothing found for your criteria.</p>';
    endif;

    // no wp_reset_query() required

    echo json_encode( array(
        'posts' => json_encode( $wp_query->query_vars ),
        'max_page' => $wp_query->max_num_pages,
        'found_posts' => $wp_query->found_posts,
        'content' => $posts_html
    ) );

    die();
}
Share Improve this question asked Apr 11, 2019 at 7:22 Adham MohamedAdham Mohamed 1853 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I haven't tested the code, but this should help.

option elements do not have a name attribute:

<option name="top-views" value="top-views">Top Views</option>
<option name="top-likes" value="top-likes">Top Likes</option>

And therefore $_POST['top-views'] for example, is not going to be set, or that the value is not the top-views in the above option.

Use the proper option markup

Based on the following markup:

<option value="comment_count-DESC">Comments ↓</option>

Your custom option should be:

<option value="{key}-{order}">{label}</option>

So (to sort in DESCending order):

<option value="top_views-DESC">Top Views</option>
<option value="top_likes-DESC">Top Likes</option>

Note that the {key} should not have a dash (-).

Parsing the sorting {key} and {order}

In the misha_insights_filter_function() function, the sorting {key} (i.e. the orderby parameter) and {order} are parsed like so:

$order = explode( '-', $_POST['misha_order_by'] );

So for example, if "Top Views" was selected, and that the option value was top_views-DESC, then $order[0] would be top_views and $order[1] would be DESC.

And you can then customize the $param like so:

if ( 'top_views' === $order[0] ) {
  $params['meta_key'] = 'post_views_count';
  $params['orderby'] = 'meta_value_num';
}

Similarly, if "Top Likes" was selected, and that the option value was top_likes-DESC, you can just copy-paste the above if block and replace the relevant values:

if ( 'top_likes' === $order[0] ) {
  $params['meta_key'] = '_post_like_count';
  $params['orderby'] = 'meta_value_num';
}

本文标签: functionsAjax posts filter by datecommentstop viewstop likes