Forked from yankiara/oxygen-repeater-dynamic-query.php
Last active
January 30, 2022 23:27
-
-
Save tdrayson/0b915b4d89615ca663f2bd1f03e0f364 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Add post object field on the CPT you want to display | |
* In the post object field select the CPT post you want this post to show on | |
* | |
* Example: | |
* 2 CPT's - Tours and Reviews | |
* | |
* To show reviews on the relevant tours: | |
* - Add the post object field to the reviews CPT | |
* - Select the tours that I want the reviews to show on inside the Review post | |
* - Add the before and after code in codeblocks before and after the repeater | |
* - On the repeater use custom query and set post type and number of posts per page as you wish) | |
*/ | |
//Before Repeater Code | |
<?php | |
function dynamic_category_query( $query ) { | |
if ( $query->query['post_type'][0] == 'CPT_SLUG' ) { | |
$query->set( 'meta_query', array( | |
array( | |
'key' => 'FIELD_NAME', //name of custom field | |
'value' => '"' . get_the_ID() . '"', | |
'compare' => 'LIKE' | |
) | |
) ); | |
$query->set( 'orderby', 'rand' ); | |
$query->set( 'no_found_rows', true ); | |
} | |
} | |
add_action( 'pre_get_posts', 'dynamic_category_query' ); | |
?> | |
/* | |
* REPEATER (use custom query and set post type and number of posts per page as you wish) | |
*/ | |
/* Put this in a code block just AFTER the repeater if you need to make other queries later on the page */ | |
<?php | |
remove_action( 'pre_get_posts', 'dynamic_category_query' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 ...