I am running a WordPress query. To determine which posts I want to display, a calculation needs to be run on each post.
To use an example, let's say all of my posts have a custom field, "my_custom_field". The value is an integer between 1 and 100.
The user then inputs a number between 1 and 100 as well. For each post, we subtract the user's number from the value of my_custom_field. It the result is larger than 10, we display that post in our results.
How do I run that query? I can't seem to figure out which direction to go with this.
Keep in mind, this is just a simplified example of a complicated problem. Assume that we are actually going to need to run a calculation for every post. I realize that in this simple example, you could solve the equation and figure out the range of values that you should display. This won't work in my real-world usage.
Here's an example of how I might want to do it. The problem is, of course, that you can't actually run functions and calculations in the $args array. This is merely to demonstrate what I'm trying to accomplish, I understand that this is not how an $args array works.
$args = array ( $a = get_field('my_custom_field'); //I'm using ACF which uses get_field, not important
$b = $user_inputted_field //The value the user entered
Display posts where: $a - $b > 10 //Not real code... Just showing my intention
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
//Display our posts
}
I've been able to enable or prevent posts from DISPLAYING by basically putting that code form my $args into the display portion of the loop, but of course that's very sloppy and messes horribly with pagination.
I need some way to run that calculation on each post in the WP_Query object and remove all posts that don't meet those criteria. How can I manually filter those results before I actually run the loop?