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


Posted

in

, ,

by

Tags:

Comments

17 responses to “How to generate query string for Easy Posts and Repeaters in Oxygen”

  1. skylermax2013 Avatar
    skylermax2013

    Great, thx!

    But how can I add an active parameter, for example:

    'post_parent' => $post->ID,

    ????

    1. Sridhar Katakam Avatar

      As far as I know, that is not supported/possible in Easy Posts element.

      1. skylermax2013 Avatar
        skylermax2013

        So sad. Anyway, thx!

  2. Nacho Avatar
    Nacho

    Is it possible to setup the easypost with ACF?
    post_type=the_field(‘easypost’)
    + setup a filter showing only posts if a second ACF ist true?

    1. Sridhar Katakam Avatar

      Can you elaborate on what exactly you are trying to do?

  3. George Avatar
    George

    Hey Sridhar, thx for the tut 🙂
    I’d like to show future events in my EasyPost module and therefore need to input the current date and a future date into the query. Is it somehow possible to preprocess the query?

    1. Sridhar Katakam Avatar

      This is not possible with Easy Posts as far as I know.

      You will have to write a custom WP query with a loop in a Code Block element.

  4. […] How to generate query string for Easy Posts in Oxygen […]

  5. Noz Avatar

    Great tutorial, thank you 🙂

  6. Victor Avatar

    Is it possible use ACF to filter Easy Post query? Let me explain what I’m trying to do:

    I have a template that I use to create some pages for Countries (each page is 1 Country).

    I also have some blog posts made for each country and I use categories to order them. (each category is a Country).

    Now I have in each page, an ACF field to set the blog category of the country I’m in.

    And what I want is to display in the template an Easy Post where I can get all the posts that belongs to the category I set on the ACF field I set in the page.

    Maybe in an example I could explain better:

    I made a template called “Countries”.

    With that template I created 2 pages: “USA” and “France”.

    Then I created a post for “New York” and another for “Philadelphia” under the category “USA category” and another post “Paris” under category “France Category”.

    I create a ACF field for pages and in “USA” page I set “USA category” and in “France” page i set “France category”.

    Now in my template for Countries, I add an Easy Post and I want it to filter for blog category so in USA page I could see only the posts “New York” and “Philadelphia” and in France Page I could only see “Paris” post.

    How can I get this? I don’t know how to filter by ACF field dynamically in Easy Post.

    Thanks!!!

  7. RAHAT Avatar
    RAHAT

    once your site is built around Oxygen, can it run with a child theme plugin?
    if not, how do you preserve all the customizations so that nothing is lost when Oxygen updates?

  8. Chris Avatar
    Chris

    found a nice resource to generate wp_query:

    https://generatewp.com/wp_query/

  9. LinkmasterMonkey Avatar
    LinkmasterMonkey

    Hi Shridhar.

    Is there a way to use the current category in a manually query?

    This is the problem I’m having now:

    I have made a CPT called ‘vacatures’ and I want to show all the ‘vacature’ items in a repeater.

    The url should be http://www.just-a-domain.com/category/chauffeur/ but then the repeater is not showin any items.

    When I use http://www.just-a-domain.com/category/chauffeur/?post_type=vacatures the repeater is working correct.

    So my idea is to make a manually query where I just want to get the items where the caregory name is the one in the url.

    Is this possible or is there a different way to do this? I’m a bit stuck here.

    Thnx,
    Leon

    1. Sridhar Katakam Avatar

      It is not currently possible to use dynamic variables or functions in manual queries of Easy Posts/Repeater in Oxygen.

      I have requested for this feature here: https://github.com/soflyy/oxygen-bugs-and-features/issues/419.

      You can try this advanced method if you want: https://gist.github.com/yankiara/c43da83662a14e7609b4e23f6d82717a.

  10. afroniquely Avatar
    afroniquely

    Is it possible to generate a query string for an existing layout? For example, if the Easy Post already was configured to show the right posts and categories/tags etc, can we use a query builder to extract the actual string of that from the Easy Post?

    I find that while easy post can give us the right posts/tags/categories, it still doesn’t yet let us do offsets and showing no paginations etc yet, so I’d still want the manual query, but most time I can’t always figure out what all the exact query strings are.

    1. Sridhar Katakam Avatar

      I am afraid there is no way to obtain a query string for an existing default/custom query.

      Getting working pagination when offset is used is not straight forward. You have to follow https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination.

Leave a Reply

Your email address will not be published. Required fields are marked *