-
-
Save yankiara/c43da83662a14e7609b4e23f6d82717a to your computer and use it in GitHub Desktop.
/* I'll put here different examples of dynamic query for Oxygen repeater : | |
* - Use one of the following repeater_dynamic_query definitions | |
* in code block just BEFORE the repeater | |
* - Set the repeater custom query settings : post type, number of posts, order... | |
* - Add the remove_action in a code block AFTER the repeater | |
*/ | |
/**************************************************************************************************** | |
* Display related posts for any CPT with taxonomy: | |
* - Filter query to prevent altering queries inside the repeater items, | |
* - Retrieve post category slug : I have only one for each post, so I just take first element | |
* (You might need to add error tests, of course, if you don't accept empty results, | |
* for instance if you forgot to set post category.) | |
* - Set tax_query arg with category slug | |
* - Set random order | |
* - Exclude current post | |
* - Deactivate pagination | |
*/ | |
/* Code block just BEFORE the repeater */ | |
<?php | |
function repeater_dynamic_query( $query ) { | |
global $post; | |
if ( $query->query['post_type'][0] == 'post' ) { | |
$cat = wp_get_post_terms( $post->ID , 'category', array( 'fields' => 'slugs' ) )[0]; | |
$query->set( 'tax_query', array( | |
array( | |
'taxonomy' => 'category', | |
'field' => 'slug', | |
'terms' => $cat, | |
'include_children' => false | |
) | |
) ); | |
$query->set( 'orderby', 'rand' ); | |
$query->set( 'post__not_in', array($post->ID) ); | |
$query->set( 'no_found_rows', true ); | |
} | |
} | |
add_action( 'pre_get_posts', 'repeater_dynamic_query' ); | |
?> | |
/* | |
* REPEATER: use custom query and set post type to "post" or any cpt slug, | |
* number of posts per page as you wish, | |
* and replace "category" by your cpt taxonomy slug if needed | |
*/ | |
/* Code block just AFTER the repeater */ | |
<?php | |
remove_action( 'pre_get_posts', 'repeater_dynamic_query' ); | |
?> | |
/**************************************************************************************************** | |
* Display only sticky posts with repeater: | |
* - Get only sticky posts | |
* - Deactivate pagination | |
*/ | |
/* Code block just BEFORE the repeater */ | |
<?php | |
function repeater_dynamic_query( $query ) { | |
if ( $query->query['post_type'][0] == 'post' ) { | |
$query->set( 'post__in', get_option( 'sticky_posts' ) ); | |
$query->set( 'no_found_rows', true ); | |
} | |
} | |
add_action( 'pre_get_posts', 'repeater_dynamic_query' ); | |
?> | |
/* | |
* REPEATER: use custom query and set post type to "post", | |
* number of posts per page and order as you wish, | |
* AND DO NOT UNCHECK "Ignore sticky posts" | |
*/ | |
/* Code block just AFTER the repeater */ | |
<?php | |
remove_action( 'pre_get_posts', 'repeater_dynamic_query' ); | |
?> | |
/**************************************************************************************************** | |
* Display posts by year: | |
*/ | |
/* Code block just BEFORE the repeater */ | |
<?php | |
function custom_query_by_year( $query ) { | |
if ( $query->query['post_type'][0] == 'YOUR_POST_SLUG' ) { | |
$query->set('date_query', [ [ 'year' => '2021' ] ] ); | |
} | |
} | |
add_action( 'pre_get_posts', 'custom_query_by_year' ); | |
?> | |
/* | |
* REPEATER | |
*/ | |
/* Code block just AFTER the repeater */ | |
<?php | |
remove_action( 'pre_get_posts', 'custom_query_by_year' ); | |
?> |
I did some experiments. This put into repeater field echoes page ID, not repeater field post ID:
MB helper function rwmb_get_value( $field_id, $args, $post_id ); then returns $field_id of incorrect $post_id. As I understand, I need to give $post_id argument correct value, then this basic query should work for custom fields.
Why does my code output all posts when it shouldn't output anything?
<?php
function repeater_dynamic_query( $query ) {
$current_id = get_queried_object_id();
$query_ids = get_term_meta( $current_id, 'дополнительная_информация', true );
global $post;
if ( $query->query['post_type'][0] == 'add_cat_info' ) {
$query->set( 'post__in', $query_ids );
$query->set( 'no_found_rows', true );
}
}
add_action( 'pre_get_posts', 'repeater_dynamic_query' );
?>
Hey guys, any tips would be appreciate on this please.
When using pre_get_posts action hook inside oxygen repeater (any custom query):
when updating $query->set('something', 'value'), it does not update his object property $this->found_posts.
This is a really strange behavior....or i missing something ...
function my_pre_get_post($query){
...
$query->set('somefilter', 'value');
$query->found_posts // 0 why is that? There is post showing up on the front end
...
}
Hello everyone, how can I display all my product categories on the shop archive page with a repeater? Thank you in advance!
You're definitely not selecting the correct field…