Conditionals Explained: Fine-Tuning Sorting appliance with Superglobals data comparison
The Advanced Post Types Order plugin offers robust conditional logic to control the application of custom sorting in WordPress. This feature is particularly useful when you want to limit the application of sorting rules to certain scenarios, such as when using AJAX filters or other dynamic content-loading techniques. One of the key aspects of these Conditionals is the use of the $_POST
$_GET
$_REQUEST
superglobals to check against the provided arguments.
What is the $_POST ( or $_GET or $_POST ) Conditional Selection?
The $_POST
conditional selection works by checking the data sent via HTTP POST requests against predefined arguments. This is especially useful in scenarios where sorting behavior needs to be dynamically controlled based on user interactions, such as AJAX-based filters. Primarily, AJAX powers the majority of the site’s dynamic filters, enhancing user interaction and seamless content updates. Example of an AJAX-based filter:
Structure of Conditionals
Conditionals in Advanced Post Types Order can be structured using groups with AND
or OR
operators:
- AND: All conditions in the group must be met for the sorting rule to apply.
- OR: At least one condition in the group must be met for the sorting rule to apply.
This flexibility allows developers to craft precise sorting rules tailored to specific needs.
Comparison Types
For $_POST
conditionals, there are two comparison types available:
- Contains: The condition is met if the
$_POST
data includes the specified value. - Not Contains: The condition is met if the
$_POST
data does NOT include the specified value.
This enables fine-tuned control over when sorting rules should or should not be applied.
Formatting the Comparison Value
When the AJAX-driven filter is used, it sends a request to the engine endpoint along with specific arguments. These arguments can be inspected using the browser’s developer console, allowing you to identify the exact parameters needed for setting up conditionals.
The value used for comparison must be formatted using the URL-encoded format
format, meaning key-value pairs should be structured as follows:
key=value&another_key=another_value
In cases where array structures need to be checked ( as to the example image above ), the format can include array notation:
option[name]=orderby&option[value]=price
Each provided value is checked against the $_POST
superglobal to determine whether the sorting rule should be executed.
Practical Applications
A common use case for the $_POST
conditional selection is limiting sorting applications in AJAX-powered filtering systems. For example:
- When using AJAX-based filters, where
Auto Apply Sort
is active, a$_POST
conditional can be used to exclude a predefined sorting order if a specific filter is applied. - This ensures that the custom order does not override the user-defined front-side preference in real-time filtering scenarios.
- The ability to conditionally bypass sorting logic ensures that sorting rules do not interfere with dynamic content updates.
Example Scenario
Imagine a WooCommerce store where users can filter products based on price, popularity, and custom sorting. If a filter is applied via AJAX with option[name]=orderby&option[value]=price
, the $_POST
conditional can be set to Not Contains
this value, ensuring that the global sorting order does not override the AJAX filter.
In this way, you can exclude specific front-end AJAX-driven filter actions from triggering the sorting logic. For instance, if a front-end filter is set to order content by Price, you can configure the conditionals to ensure that a specific custom order does not apply in this scenario. This is particularly useful when you want to maintain the default sorting behavior for certain filters while applying custom sorting rules in other cases.
The $_POST
conditional selection in Advanced Post Types Order provides a powerful way to fine-tune sorting logic in dynamic WordPress environments. By leveraging conditionals with structured formatting, comparison operators, and logical grouping, developers can ensure that sorting behavior aligns seamlessly with user interactions and filtering mechanisms.