/* 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' ); | |
?> |
This comment has been minimized.
This comment has been minimized.
Hi @Jesurun, |
This comment has been minimized.
This comment has been minimized.
Newbie mistake. I used the same function name in both code blocks. It's all good now, thank you for checking back. |
This comment has been minimized.
This comment has been minimized.
Hi @yankiara I'm not a PHP developer, just a hobbyist hack trying to get something working on my site, so apologies if I'm misunderstanding something here. I sort of get what your code is doing and I think I need something similar: I have dozens of pages on my Oxygen site that all need a repeater with the identical query EXCEPT that one variable (an Advanced Custom Field called "Episode") from the post needs to be the filter term in the query. Example... POSTS (where the template with this repeater will be applied):
CUSTOM POSTS (these will be queried in the repeater)
So when Post One is called up, it will show Custom Posts A and B within it. All I want to do is make my repeater query filter by this Episode variable (which changes based on which post is called). I played around with this idea: In other words, I don't know what the hell I'm doing. :) But I think your code to set the $query variable dynamically is what I need, I just don't understand how to get from where I am to your code. I'm simply trying to prevent making a reusable element (a repeater) that just has a single variable different in each usage. It's just not easy to maintain dozens of copies if I need to change something about the look/etc. Any help would be appreciated! Thanks... |
This comment has been minimized.
This comment has been minimized.
Hi @thinkmediaco,
|
This comment has been minimized.
This comment has been minimized.
Hi @yankiara Thanks so much! Your reply made me realize that I actually need something even simpler:
RESULTS:
Thoughts? Did I screw up the syntax or miss something? |
This comment has been minimized.
This comment has been minimized.
Well if By the way if the tag was a custom taxonomy, I think your syntax would be wrong because you need to define the tax slug like I do with category_slug. But I've actually never tried this with tags so I may be mistaken. |
This comment has been minimized.
This comment has been minimized.
Is the check in L17,
necessary? I tested this with a couple of secondary queries on a Page and it is working without that check because It would be needed if we want to target the query in a Code Snippet or a custom functionality plugin and even then it won't be unique because there could be more queries using the same post type on the targeted page. |
This comment has been minimized.
This comment has been minimized.
You may be right, but I remember having troubles using functions that make their own WP query inside the repeater, because pre_get_posts() also changed it. Maybe there's no problem with wp_get_post_terms(). |
This comment has been minimized.
This comment has been minimized.
Hi @srikat, |
This comment has been minimized.
This comment has been minimized.
Hi folks! Anybody can help me out, how to query in this way acf relationship post object? I have a custom post type where user can select other posts from acf relationship fields, and i need to put this posts into a repeater. It would be lifesaver. Thanx! |
This comment has been minimized.
This comment has been minimized.
Can this be used to query 'Users' and 'User Roles', etc.? |
This comment has been minimized.
This comment has been minimized.
I doubt about it, since there are other WP functions to retrieve users: get_users() and wp_user_query(). But this is a very good question and that might be a useful feature request (using wp_user_query in repeater). |
This comment has been minimized.
This comment has been minimized.
How would i use this to query just the parent posts in a post type? Also does it work with Facetwp? |
This comment has been minimized.
This comment has been minimized.
@yankiara And how would i get this to just query the child posts of the current post? |
This comment has been minimized.
This comment has been minimized.
@jamescrabb Sorry this is out of the scope here ;-) |
This comment has been minimized.
This comment has been minimized.
@yankiara |
This comment has been minimized.
This comment has been minimized.
@jamescrabb > How would i use this to query just the parent posts in a post type? Also does it work with Facetwp? Are you trying to get top level pages or only pages that are parents? To then get child pages, Sridhar has a tutorial on WPDevDesign - [https://wpdevdesign.com/subpages-grid-in-oxygen/] |
This comment has been minimized.
This comment has been minimized.
I have a custom query to show posts based on an array of post ID's in a users meta data...but I can't work out how to convert it to place in this function. Any suggestions? Here's the code that I have to create the loop to pull in all posts indicated by the user array: `<?php
?>` |
This comment has been minimized.
This comment has been minimized.
Yes i have used post_parent=0 It does appear to be only working on the taxonomy terms not the main archive. |
This comment has been minimized.
This comment has been minimized.
@UrbanFugitive, you can try something like this:
And don't forget to set your repeater to custom query and choose post. |
This comment has been minimized.
This comment has been minimized.
Can this be used to display relationship fields from ACF? My oxygen repeater is pulling from a "lessons" post type and currently displaying all lessons. I only need to show lessons that are related to the current post id. The relationship field is as follows:
|
This comment has been minimized.
This comment has been minimized.
@mdsims086 > Can this be used to display relationship fields from ACF? My oxygen repeater is pulling from a "lessons" post type and currently displaying all lessons. I only need to show lessons that are related to the current post id. The relationship field is as follows Try this https://gist.github.com/tdrayson/0b915b4d89615ca663f2bd1f03e0f364 |
This comment has been minimized.
This comment has been minimized.
@tdrayson > Try this https://gist.github.com/tdrayson/0b915b4d89615ca663f2bd1f03e0f364 Can't seem to get it to work. Code block is causing the repeater to display nothing. Here's the code..
Repeater is set to display CPT "lessons" |
This comment has been minimized.
This comment has been minimized.
@mdsims086 I haven't got it working with the relationship field. I used it with the Post object field between 2 CPT's You may be able to modify it to work with the relationship field. Is it 2 way relationship or 1 way? |
This comment has been minimized.
This comment has been minimized.
@tdrayson its a 1 way relationship |
This comment has been minimized.
This comment has been minimized.
@mdsims086 Should be similar then. Maybe have a look at ACF docs |
This comment has been minimized.
This comment has been minimized.
Having an issue with the Category part working. Is anyone able to confirm whether it is working for them? It returns "internal server error 500" However, if I remove the category part then the query works fine. |
This comment has been minimized.
This comment has been minimized.
I know this is probably redundant or obvious for many people watching this but this took me like 1 week to figure out.
|
This comment has been minimized.
This comment has been minimized.
@StavrosWTF it really works. Thank you |
This comment has been minimized.
This comment has been minimized.
I know what I want to do isn't really anything to do with creating a 'related posts' page, but I have a feeling I need to be using a dynamic query in a similar way to this, and I was hoping you could help as you seem to know Oxygen repeaters so well. I have a custom post type called 'sessions' and I want to have a repeater that just shows the current session (but only if there is one). I have 2 custom fields, 'session_start_time' and 'session_end_time' (both datetime fields) and I know I need to basically get it to only show the repeater if 'session_start_time' has passed, but 'session_end_time' hasn't. Problem is I'm not really a coder and don't know php well enough to figure it out on my own. Any help would be greatly appreciated. :D |
This comment has been minimized.
This comment has been minimized.
Something like this should work @hamish-strachan although haven't tested. Let me know how you get on.
|
This comment has been minimized.
This comment has been minimized.
I get an error from that code: Parse error: syntax error, unexpected 'array' (T_ARRAY), expecting ')'code-block.class.php(115) : eval()'d code on line 11 |
This comment has been minimized.
This comment has been minimized.
I missed a comma after the first key. Try copying the code again. I have just adjusted it. |
This comment has been minimized.
This comment has been minimized.
What I've learn so far about php is that it's always a missing comma!! Still doesn't seem to be working, although I don't get an error any more |
This comment has been minimized.
This comment has been minimized.
Did you update the CPT_SLUG section? What format is your date field in? |
This comment has been minimized.
This comment has been minimized.
yeah, I'm guessing that should be 'session' |
This comment has been minimized.
This comment has been minimized.
Is it possibly because 'type' is set to date? My fields use DATETIME. |
This comment has been minimized.
This comment has been minimized.
Sorry, missed the question about the date field... I think it's in Y-m-d H:i:s, isn't that the default for custom fields datetime? |
This comment has been minimized.
This comment has been minimized.
Yes you might need to change the date format and the type to what you have. |
This comment has been minimized.
This comment has been minimized.
Already tried and it's still not working :/ |
This comment has been minimized.
This comment has been minimized.
Do you want to join the Discord channel https://discord.gg/HHaQ7judq5 will be easier to help debug |
This comment has been minimized.
This comment has been minimized.
Thanks, got it sorted in the end. The < and > needed swapping, and I realised I needed another variable in there too, but this code is now working...
|
This comment has been minimized.
This comment has been minimized.
I've been struggling with something very similar, and it's now working thanks to this thread :) |
This comment has been minimized.
This comment has been minimized.
Thank you @yankiara for sharing the code! Is it possible to modify this function to show only the current user's posts inside the Repeater? I need to create a frontend dashboard where users can see only their own posts. |
This comment has been minimized.
This comment has been minimized.
OK, I'll answer my previous question. I found out how to output only posts whose author is the current user. This is a simple code that does only that 1 thing. If the current user doesn't have a single post, the repeater won't show anything. But, if the current user isn't logged in, all posts will be displayed. Please, tweak this setting as you wish. <?php
function mdtrn_current_user_posts_query( $query ) {
global $post;
global $user_ID;
if ( $query ) {
$query->set('author', $user_ID );
}
}
add_action( 'pre_get_posts', 'mdtrn_current_user_posts_query' );
?> |
This comment has been minimized.
This comment has been minimized.
@Mediteranija You can use Oxygen conditions. If logged in show the repeater. If logged out then hide the repeater. If current user posts = 0 then hide the repeater and show text block that says "You have no posts" |
This comment has been minimized.
This comment has been minimized.
@tdrayson Yes, I'm aware of that. But I wanted to tweak the repeater itself to output only current user's posts (if any). |
This comment has been minimized.
This comment has been minimized.
Hi @tdrayson Pleas help me. |
This comment has been minimized.
This comment has been minimized.
HI @Wpaid, Can you explain what you mean by the code does not work? You added the dynamic data and it shows in the website. However you said that "it doesn't works because it only sorted by date" and you want it to order by Random? |
This comment has been minimized.
This comment has been minimized.
@tdrayson |
This comment has been minimized.
This comment has been minimized.
@Wpaid |
This comment has been minimized.
This comment has been minimized.
@tdrayson |
This comment has been minimized.
This comment has been minimized.
Did you update the code so you changed the taxonomy slug and changed the post type slug? |
This comment has been minimized.
This comment has been minimized.
Is there a chance we could see a very basic example of this ingenius code getting related Woocommerce products by attribute (pa_year)? Or even just products from same category. It would be much appreciated! |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
I'm still having issues with this. |
This comment has been minimized.
This comment has been minimized.
Nested inside another repeater is not working, I used toolset relationship to connect two posts. |
This comment has been minimized.
This comment has been minimized.
Yes, here it works with 3.7! |
This comment has been minimized.
This comment has been minimized.
Hey guys, Can you please help me how to modify the code for WooCommerce products? I need to list all the products in the "pizzy" category and sort them by "menu_order". I tried to modify the code, but Oxygen always throws me a 500 error and it can't be saved. P.S .: After inserting the code into the "code block" and applying. "Hello world" will not disappear either. Unfortunately, I absolutely don't know where I'm making a mistake. |
This comment has been minimized.
This comment has been minimized.
Thanks for this piece of code, it's half working for me and I can't figure it out, hopefully somebody can help me: . Using Oxygen 3.7 . I've added the code block before my repeater . When I initially run the code without the "add_action ( 'pre_get_posts', 'filter_plans' );", it works fine, I can see all the records. . When I run the code with the "add_action ( 'pre_get_posts', 'filter_plans' );", I can see that the filter works as I can see the rows on the output but none of the fields print out, the rows are blank. I re-created the repeater from scratch and the syntax looks fine to me: [oxygen data='custom_acf_content' settings_path='contacts_min'] -- [oxygen data='custom_acf_content' settings_path='contacts_max'] Any idea why the filters would work but the fields wouldn't print? Many thanks, |
This comment has been minimized.
This comment has been minimized.
Ok, well, I can answer my own questions above, I guess it's an ACF problem as if I insert the field through the "Custom Field/Meta Options" rather than through the "Advanced Custom Field", it works fine. In short: [oxygen data='meta' key='contacts_max'] works while [oxygen data='custom_acf_content' settings_path='contacts_max'] doesn't. |
This comment has been minimized.
This comment has been minimized.
Hello! I have a question, regarding this. I need to put the code block inside a code snippet, as I have WPGrid running and the faces using AJAX, so putting it on the page, won't work correctly. I have the custom params in the repeater element: I then have following in a code snippet: `function dynamic_category_query999( $query ) {
} The question I now have: Thanks very much again! |
This comment has been minimized.
This comment has been minimized.
Hi @ALL, how if I want to include all categories there? |
This comment has been minimized.
This comment has been minimized.
Thank you very much. It works very well for me! |
This comment has been minimized.
This comment has been minimized.
That would be amazing!! :( |
This comment has been minimized.
This comment has been minimized.
I heard this feature will release on Oxygen 3.8, Amazing! |
This comment has been minimized.
This comment has been minimized.
I have solved this ....contact me if you still need to know how to do it (so we not troll this post...) -> skp: |
This comment has been minimized.
Hey Yan, this is awesome. Is it possible to use it twice in the same template?
In my case use, I have the CPT called products with Custom Taxonomies product_categories and product_manufacturer
I want to show Related Products, by pulling products from the same product_categories, and after that I want to show Products From The Manufacturer (product_manufacturer).
I am using the remove_action function after both repeaters, but I am getting an error "There has been a critical error on your website." on the front end. What do you think might be the problem?