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 the code examples out there are given as an 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.

Refereces:

https://github.com/luetkemj/wp-query-ref

https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

Need help implementing a tutorial in your site or want to hire me for custom work?

HIRE ME

Find the article helpful and wish to donate?

DONATE

Comments

For adding code blocks wrap the code in three backticks. Markdown should work.
Provide a URL of your site/webpage if something is not working.
  • 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?

  • 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?

  • 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!!!

  • magnifiercross
    >
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
    %d bloggers like this: