Array Query
Utilizing the Array Query in Loop Elements
This article briefly explains the process of using the array query feature within loop elements to create advanced content queries. This method leverages PHP for dynamic content display customization.
Setting Up Your Loop Element
Adding a Loop Element to Your Page
- Begin by adding a loop element to your page.
- Navigate to the query option in the properties panel of your selected loop element.
- Select “Array” query option and then click the expand icon to open up the query editor.
Creating Advanced Queries with Array Query
Using the PHP Editor for Custom Queries
Click the expand icon to open the PHP editor. Within the editor, you can enter PHP functions and custom code or utilize WordPress classes to craft the query. This powerful feature allows for a wide range of query customizations beyond standard options.
You can learn more about queries in WordPress through the WordPress Developer Document for WP_Query
The Oxygen Support team cannot help create these queries, here are some Array Query examples that may be useful:
Show posts authored by currently logged in/viewing user
return [
'post_type' => 'post',
'author' => get_the_author_ID()
];
Show posts by author of currently viewed post
return [
'post_type' => 'post',
'author' => get_post_field( 'post_author' )
];
tax_query example, used on archive to get current queried term
return [
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => get_queried_object()->term_id
)
)
];
Make sure the date field returns in Ymd format
return [
'post_type' => 'post',
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => 'date',
'meta_query' => array(
'key' => 'date',
'value' => date('Ymd'),
'type' => 'DATE',
'compare' => '>='
)
];
Show only children of current post
return [
'post_type' => get_post_type(),
'post_parent' => get_the_ID()
];
Show posts based on ACF Taxonomy field
return [
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => get_field('acftax'),
'operator' => 'IN',
),
),
];
Show list of posts connected to the current post by Meta Box Relationship
return [
'post_type' => 'lesson',
'relationship' => [
'id' => 'courses',
'to' => get_queried_object()->ID
],
'orderby' => 'meta_value',
'meta_key' => 'select_2caoexzgghd',
'order' => 'ASC'
];
Show posts from Search Query
return [
's' => get_search_query()
];
Get the current Taxonomy and Term
$taxonomy = get_queried_object()->taxonomy;
$term = get_queried_object()->term_id;
return [
'post_type' => 'any',
'tax_query' => array(
array(
'field' => 'id',
'taxonomy' => $taxonomy,
'terms' => $term,
)
),
];
Show All Other Posts That Have The Same Taxonomy Term(s) as the Current Post
$taxonomy_name = 'genre'; // enter the name of the taxonomy you would like to filter here
$taxonomy_field = 'slug'; // type the taxonomy field you would like to compare
$term_obj_list = get_the_terms( $post->ID, $taxonomy_name );
$terms_string = join(', ', wp_list_pluck($term_obj_list, $taxonomy_field));
return ['post_type' => 'any',
'tax_query' => array(
array(
'taxonomy' => $taxonomy_name,
'field' => $taxonomy_field,
'terms' => $terms_string
)
)
];
Handling Errors in Your Query
If you encounter a 500 error when using the query editor, this typically indicates a syntax or logical error within your PHP query. Review and correct the query to resolve the issue and refresh the query to see the corrected output.
Additional Notes
The array query feature in Oxygen offers an advanced method for customizing the content displayed within loop elements. By directly manipulating query parameters through PHP, users can achieve highly tailored content displays that meet specific site requirements. Always ensure to double-check your PHP syntax and logic to prevent errors and ensure smooth functionality.