12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Mar 25, 2019

manual Query Params in Oxygen

Sridhar Katakam

Scenario:

Custom Post type: animals

Associated custom taxonomy: animal_type

Sample taxonomy term: dogs

Now you want to display all animals that are dogs in a random order in Oxygen‘s Easy Posts element.

Since random order requirement is not supported in Easy Posts’ custom tab, we would need to use the manual mode.

The default value of Query Params in manual mode is:

author_name=admin&category_name=uncategorized&posts_per_page=2

We need to come up with a query string similar to the above but that meets our requirements.

Here’s how this can be done:

Step 1

Refer to https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters for an example of how to write WordPress Query involving custom taxonomy.

$args = array(
	'post_type' => 'post',
	'tax_query' => array(
		array(
			'taxonomy' => 'people',
			'field'    => 'slug',
			'terms'    => 'bob',
		),
	),
);
$query = new WP_Query( $args );

Based on the above, our query would be:

$args = array(
	'post_type' => 'animals',
	'orderby' => 'rand',
	'tax_query' => array(
		array(
			'taxonomy' => 'animal_type',
			'field'    => 'slug',
			'terms'    => 'dogs',
		),
	),
);

$query = new WP_Query( $args );

Step 2

In Oxygen editor (doesn’t matter which Page or Template is being edited), add a Code Block component (temporarily).

PHP & HTML:

<?php
    $args = array(
	'post_type' => 'animals',
	'orderby' => 'rand',
	'tax_query' => array(
		array(
			'taxonomy' => 'animal_type',
			'field'    => 'slug',
			'terms'    => 'dogs',
		),
	),
);
    echo build_query( $args );
?>

We are simply copying the query built at the end of earlier step and pasting it inside the opening and closing PHP tags with an additional echo build_query( $args );.

View the Frontend and copy the generated string.

It should be (in this example):

post_type=animals&orderby=rand&tax_query%5B0%5D%5Btaxonomy%5D=animal_type&tax_query%5B0%5D%5Bfield%5D=slug&tax_query%5B0%5D%5Bterms%5D=dogs

This is the query string that goes in Easy Posts’ manual mode to pull all the entries in animals CPT that have been categorized under dogs custom taxonomy in a random order.

Step 3

Delete the Code Block component.

Reference

How to generate query string for Easy Posts in Oxygen

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right