How to generate query string for Easy Posts and Repeaters in Oxygen
When you want to write a manual query in an Easy Posts element, the input needs to be a valid query string.
Almost all the code examples out there are given as arrays that contain key-value pairs like the following:
array(
'post_type' => 'page',
'post__in' => array( 2, 5, 12, 14, 20 ),
)
The above will fetch Pages that have the specified IDs.
But how do we convert the above to a query string that can be added in Easy Posts?
WordPress’ build_query function to the rescue.
Here’s how this can be used:
Step 1
Add a Code Block element (doesn’t matter where – this is just a temporary thing) and inside the PHP & HTML, add
<?php
$data = array(
'post_type' => 'page',
'post__in' => array( 2, 5, 12, 14, 20 ),
);
echo build_query( $data );
?>
Replace
array(
'post_type' => 'page',
'post__in' => array( 2, 5, 12, 14, 20 ),
)
with your custom query.
Step 2
Save the entry and view it on frontend. Copy the generated code.
Step 3
Edit your Easy Posts element. Go to Query > manual, paste the code.
Apply Query Params.
Easy Posts should now use the query you provided.
Step 4
Delete the Code Block.
References
https://github.com/luetkemj/wp-query-ref
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters