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

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.
  • magnifiercross
    >
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
    %d bloggers like this: