• Resolved aslynn

    (@anyalynn)


    I am looking for a way to limit the total number of posts returned by the query (not the # per page). Since there doesn’t seem to be an easy way to do this, I tried to create a custom callback function that would at least limit them going back 2 months. I tried creating this function (I assume that I put this in my functions.php?)

    function qw_date_filter_callback($args, $filter){
    	$args = array(
    	'date_query' => array(
    		array(
    			'after'     => 'January 1st, 2018',
    			'before'    => array(
    				'year'  => 2018,
    				'month' => 3,
    				'day'   => 28,
    			),
    			'inclusive' => true,
    		),
    	  ),
    	);
    
      return $args;
    
    }

    I tried using it as a filter in both a callback function and in the Post_ID filter like this: function qw_date_filter_callback($args, $filter){ return $args; }

    As just a callback function I still get all of the posts. As a callback for the Post_IDs inclusive, I get no posts.

    I must not understand how to use this function and I’m hoping someone might know.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Jonathan Daggerhart

    (@daggerhart)

    Hi @anyalynn,

    Your code is really close to working, I’ll try to fill in the gaps.

    Should this go in your functions.php? – It can! The active theme’s functions.php are loaded very early in WordPress bootstrap. A better approach would be to put this code into a plugin, so that it would survive a future theme update or change. But in the theme’s functions.php will work.

    Here a working version of your code:

    
    function qw_date_filter_callback($args, $filter){
    	$args['date_query'] = array(
    		array(
    			'after'     => 'January 1st, 2018',
    			'before'    => array(
    				'year'  => 2018,
    				'month' => 3,
    				'day'   => 28,
    			),
    			'inclusive' => true,
    		),
    	);
    
    	return $args;
    }
    

    The main difference is that it doesn’t replace the entire $args array, it just adds the date_query key and values.

    Inside of Query Wrangler, in the callback field you only need to provide the name of your function. In this case, just type qw_date_filter_callback into the text field on the filter.

    That should get you going! Let me know if anything is unclear or you have further trouble.

    Thread Starter aslynn

    (@anyalynn)

    Thank you so much. It is working now.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom Callback Query?’ is closed to new replies.