0
function posts_limit_fn($limit, $query, $custom_limit = ''){
    if($custom_limit !== ''){
        return $custom_limit;
    }
    return $limit;
}

this piece of code is executed into an AJAX function add_action('wp_ajax_search_query', 'search_query');

$queryString = array( 's' => $searchTerm, 'cat' => $categoriesIDs, 'post__not_in' => array($excludedPost));

if($queryString != ''){
    add_filter('post_limits', 'posts_limit_fn',10, 3);
    $custom_limit = 'LIMIT 0,4';
    apply_filters('post_limits', '', '', $custom_limit);

    $query = new WP_Query($queryString);
}

For some kinda of reasons the apply_filters doesn't work so the query is returning all posts. What would be the problem ?

UPDATE:

Seems that If I have something like this :

function posts_limit_fn($limit,$query,$custom_limit = ''){
    echo $custom_limit.">>>>>>>";
    if($custom_limit !== ''){
        return 'LIMIT 0,3';
    }
    return $limit;
}

the output is LIMIT 0,4>>>>>>>>>LIMIT 0,10>>>>>>>>>post1,post2,post3

6
  • What exactly do you want to do? Sep 5, 2014 at 16:48
  • If a moderator is somewhere around here, should I delete the first part of my post ? Sep 5, 2014 at 16:55
  • 1
    $mylimit should be an int, and it's not related with the parameters the function uses.
    – Tomás Cot
    Sep 5, 2014 at 16:58
  • If you're using query_posts I strongly recommend against it, and advise instead you make use of either get_posts or WP_Query ( all 3 use WP_Query internally )
    – Tom J Nowell
    Sep 5, 2014 at 17:06
  • You can edit your question if you like. Sep 5, 2014 at 17:06

1 Answer 1

2

The third parameter for add_filter is the priority in which the filter is executed, the forth is the number of parameters the function in the second parameter uses.

The wpcodex_filter_main_search_post_limits function has an if that checks uses a couple of wordpress function to establish where in your page you are executing the code, in this case it's checking if it's in the frontend, if it's the main query and if it's in the search page.

The parameter $limit and $query are passed by the CMS, and those are default values, so if the conditions in the if aren't met, the function return the default $limit

edit:

function posts_limit_fn($limit, $query){
    return 'LIMIT 0, 3';
}
add_filter('posts_limit', 'posts_limit_fn',10, 2);

edit 2:

if you aren't going to use the parameters you can use the function like this:

global $custom_limit;
$custom_limit = '0, 2';
    function posts_limit_fn(){
            global $custom_limit
            return 'LIMIT ' . $custom_limit;
        }
add_filter('posts_limit', 'posts_limit_fn',10, 0);

That's one way to do it. Try to prefix all global vars, always, and if you can avoid them, better yet.

edit: without globals

function posts_limit_fn($limit, $query, $custom_limit = ''){
                if($custom_limit !== ''){
                    return $custom_limit;
                }
                return $limit;
            }
    add_filter('posts_limit', 'posts_limit_fn',10, 3);
$custom_limit = 'LIMIT 0,4';

apply_filters('posts_limit', '', '', $custom_limit)

edit for WP_Query:

If you are using WP_Query, for the pagination you can pass a paged parameter with each call to the function.

7
  • The filter name is post_limits but even if I change that, it doesn't work. Does the fact that I'm doing this into an AJAX call make this to not work ? Sep 6, 2014 at 6:17
  • Check the updated question . What's strange is that if I replace return $custom_limit with return 'LIMIT 0,3' it's works. Sep 6, 2014 at 12:08
  • @George02, if you are using a WP_Query, why not just add the posts_per_page parameter?
    – Tomás Cot
    Sep 6, 2014 at 12:29
  • Because I'm doing this for an ajax widget. So first I want to show the first 5 results then at a button press to show the next 5. It's possible to do this with post_per_page ? Sep 6, 2014 at 12:52
  • seems that calling add_filter() and then apply_filters() the function posts_limit_fn is called twice and in one of those 2 calls the $custom_limit is empty. What about that ? Sep 6, 2014 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.