Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use dynamic queries with Oxygen's repeater
/* 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' );
?>
@Jesurun

This comment has been minimized.

Copy link

@Jesurun Jesurun commented Feb 13, 2020

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?

@yankiara

This comment has been minimized.

Copy link
Owner Author

@yankiara yankiara commented Feb 25, 2020

Hi @Jesurun,
Sorry I was on holydays with no connection ;-)
I don't see why it wouldn't work. I remember testing it and it was OK.
Have you solved the problem in the meantime?

@Jesurun

This comment has been minimized.

Copy link

@Jesurun Jesurun commented Mar 4, 2020

Newbie mistake. I used the same function name in both code blocks. It's all good now, thank you for checking back.

@thinkmediaco

This comment has been minimized.

Copy link

@thinkmediaco thinkmediaco commented May 16, 2020

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):

  • Post titled One (where the ACF Episode variable = "post_one")
  • Post titled Two (Episode = "post_two")
  • Etc.

CUSTOM POSTS (these will be queried in the repeater)

  • Custom Post A (which is has the tag "post_one")
  • Custom Post B (which also has the tag "post_one")
  • Etc

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:
$episode = get_field('episode');
then tried to set the 'tag' in the $args array to be $episode
but i didn't know how to feed that back to make it become the repeater query

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...

@yankiara

This comment has been minimized.

Copy link
Owner Author

@yankiara yankiara commented May 18, 2020

Hi @thinkmediaco,
You can filter/sort queries by cutom field using meta_key and meta_value (meta_value_num for numeric fields).
For instance, the following query args filter properties (CPT) whose status is not sold and sort them by decreasing price:

    $query->set( 'meta_query',
                array(
                  array(
                    'key' => 'status',
                    'value' => 'Sold',
                    'compare' => '!=',
                  )
                ) );
    $query->set( 'orderby', 'meta_value_num' );
    $query->set( 'meta_key', 'price' );
    $query->set( 'order', 'DESC' );
@thinkmediaco

This comment has been minimized.

Copy link

@thinkmediaco thinkmediaco commented May 19, 2020

Hi @yankiara

Thanks so much! Your reply made me realize that I actually need something even simpler:

  • The page this goes on has an ACF called 'episode_number_text' (example value = "ef47")
  • Then I have a CPT called "quotes".
  • I want to display all quotes that have a tag matching that ACF field (e.g. all "quotes" posts with the tag "ef47")
  • So here's what I've tried:
/* I put this code block just BEFORE the repeater */
<?php
   $episode = get_field('episode_number_text');

  function dynamic_quotes_query( $query ) {
     $query->set( 'tax_query', array ('tag' => $episode) );
     $query->set( 'order', 'DESC' );
} 

add_action( 'pre_get_posts', 'dynamic_quotes_query' );
?>

/*  Then the only thing in the repeater is a Custom query with the post type set to "quotes"  */

RESULTS:

  • The results are that the page simply shows ALL of the quotes, instead of filtering it down to only those with the tag "ef47".
  • Note that I do an echo of the $episode variable, I can see that it's getting the variable correctly from the page (e.g. "ef47")
  • Note also that if I do this manually in the repeater query (e.g. "post_type=quotes&tag=ef47"), it filters them correctly
  • So I assume something must be wrong with my version (dynamic_quotes_query) of your tax_query function?

Thoughts? Did I screw up the syntax or miss something?
Thanks again for looking... much appreciated.

@yankiara

This comment has been minimized.

Copy link
Owner Author

@yankiara yankiara commented May 20, 2020

Well if post_type=quotes&tag=ef47 works, then I assume tag is the standard post tag, so you don't need to use tax_query.
Just try query->set( 'tag' => $episode ); and see how it works (I haven't tested).

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.

@srikat

This comment has been minimized.

Copy link

@srikat srikat commented May 25, 2020

Is the check in L17,

if ( $query->query['post_type'][0] == 'cpt_slug' ) {

necessary?

I tested this with a couple of secondary queries on a Page and it is working without that check because dynamic_category_query function is being removed from the pre_get_posts action.

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.

@yankiara

This comment has been minimized.

Copy link
Owner Author

@yankiara yankiara commented May 31, 2020

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().

@yankiara

This comment has been minimized.

Copy link
Owner Author

@yankiara yankiara commented Jun 20, 2020

Hi @srikat,
Condition is definitely needed in some cases. For instance a product query with ACF field ordering.
There must be some internal query that breaks everything if it not protected (maybe ACF runs query to get field value?).

@c001os

This comment has been minimized.

Copy link

@c001os c001os commented Aug 11, 2020

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!

@AdventurouslySocial

This comment has been minimized.

Copy link

@AdventurouslySocial AdventurouslySocial commented Aug 26, 2020

Can this be used to query 'Users' and 'User Roles', etc.?

@yankiara

This comment has been minimized.

Copy link
Owner Author

@yankiara yankiara commented Aug 27, 2020

Can this be used to query 'Users' and 'User Roles', etc.?

I doubt about it, since there are other WP functions to retrieve users: get_users() and wp_user_query().
And I couldn't find any example of wp_query getting users.

But this is a very good question and that might be a useful feature request (using wp_user_query in repeater).

@jamescrabb

This comment has been minimized.

Copy link

@jamescrabb jamescrabb commented Oct 12, 2020

How would i use this to query just the parent posts in a post type? Also does it work with Facetwp?

@jamescrabb

This comment has been minimized.

Copy link

@jamescrabb jamescrabb commented Oct 13, 2020

How would i use this to query just the parent posts in a post type? Also does it work with Facetwp?

@yankiara And how would i get this to just query the child posts of the current post?

@yankiara

This comment has been minimized.

Copy link
Owner Author

@yankiara yankiara commented Oct 14, 2020

@jamescrabb Sorry this is out of the scope here ;-)
But I think I would use get_ancestors() and get_children() functions to retrieve a list of ids, then use post_parent__in and post__in argument.

@jamescrabb

This comment has been minimized.

Copy link

@jamescrabb jamescrabb commented Oct 14, 2020

@jamescrabb Sorry this is out of the scope here ;-)
But I think I would use get_ancestors() and get_children() functions to retrieve a list of ids, then use post_parent__in and post__in argument.

@yankiara
Ok so im gonna be honest im useless with php how would i go about writing this? And could it be used in the same fashion to effect the repeater element?

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Oct 18, 2020

@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?
If you only want to return top level pages (regardless if it has child pages) you can use [post_parent=0] in the custom query section

To then get child pages, Sridhar has a tutorial on WPDevDesign - [https://wpdevdesign.com/subpages-grid-in-oxygen/]

@UrbanFugitive

This comment has been minimized.

Copy link

@UrbanFugitive UrbanFugitive commented Oct 20, 2020

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
$ids = get_user_favorites($user_id);

$args = array(
'post__in' => $ids
);

$query = new WP_Query( $args );
	if($query->have_posts() ) {
	 	while($query->have_posts() ) {
    	$query->the_post();
    	
    	}
	}

?>`

@jamescrabb

This comment has been minimized.

Copy link

@jamescrabb jamescrabb commented Oct 20, 2020

@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?
If you only want to return top level pages (regardless if it has child pages) you can use [post_parent=0] in the custom query section

To then get child pages, Sridhar has a tutorial on WPDevDesign - [https://wpdevdesign.com/subpages-grid-in-oxygen/]

Yes i have used post_parent=0

It does appear to be only working on the taxonomy terms not the main archive.

@yankiara

This comment has been minimized.

Copy link
Owner Author

@yankiara yankiara commented Oct 20, 2020

@UrbanFugitive, you can try something like this:

<?php
function dynamic_ids_query( $query ) {
  global $post;
  if ( $query->query['post_type'][0] == 'post' ) {
    $ids = get_user_favorites($user_id);
    $query->set( 'post__in', $id );
    $query->set( 'orderby', 'rand' ); /* Here you can change order */
    $query->set( 'no_found_rows', true ); /* Delete this if you need pagination */
  }
}
add_action( 'pre_get_posts', 'dynamic_ids_query' );
?>

And don't forget to set your repeater to custom query and choose post.

@mdsims086

This comment has been minimized.

Copy link

@mdsims086 mdsims086 commented Nov 2, 2020

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:

<?php $related_lessons = get_field( 'related_lessons' ); ?>
<?php if ( $related_lessons ) : ?>
	<?php foreach ( $related_lessons as $post_ids ) : ?>
		<a href="<?php echo get_permalink( $post_ids ); ?>"><?php echo get_the_title( $post_ids ); ?></a>
	<?php endforeach; ?>
<?php endif; ?>
@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Nov 2, 2020

@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

@mdsims086

This comment has been minimized.

Copy link

@mdsims086 mdsims086 commented Nov 2, 2020

@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..

<?php
function dynamic_category_query( $query ) {
  if ( $query->query['post_type'][0] == 'lessons' ) {
    $query->set( 'meta_query', array(
                                array(
                                  'key' => 'related_lessons', //name of custom field
                                  'value' => get_the_ID(),
                                  'compare' => 'LIKE'
                                )
                              ) );

    $query->set( 'no_found_rows', true );
  }
}
add_action( 'pre_get_posts', 'dynamic_category_query' );
?>

Repeater is set to display CPT "lessons"
Content is being shown on template for CPT "modules" that contains the "related_lessons" field

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Nov 2, 2020

@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?

@mdsims086

This comment has been minimized.

Copy link

@mdsims086 mdsims086 commented Nov 2, 2020

@tdrayson its a 1 way relationship

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Nov 2, 2020

@mdsims086 Should be similar then. Maybe have a look at ACF docs

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Nov 23, 2020

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.

@StavrosWTF

This comment has been minimized.

Copy link

@StavrosWTF StavrosWTF commented Dec 8, 2020

I know this is probably redundant or obvious for many people watching this but this took me like 1 week to figure out.
I have seen many newbies like me (with little to no knowledge of php) asking on the fb group how they can create a related posts section.
So if somebody needs a Related Posts section in their single post template just use this code here:

function dynamic_category_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', 'dynamic_category_query' );
?>```
@spl1t

This comment has been minimized.

Copy link

@spl1t spl1t commented Dec 13, 2020

@StavrosWTF it really works. Thank you

@hamish-strachan

This comment has been minimized.

Copy link

@hamish-strachan hamish-strachan commented Dec 15, 2020

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

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Dec 15, 2020

Something like this should work @hamish-strachan although haven't tested. Let me know how you get on.

//Before Repeater Code
<?php
function dynamic_category_query( $query ) {
  if ( $query->query['post_type'][0] == 'CPT SLUG' ) {
    $query->set( 'meta_query', array(
                                array(
                                  'key' => 'session_start_time',
                                 'value' => date('Ymd'),
		                 'type' => 'DATE',
		                 'compare' => '>='
                                ),
                               array(
                                  'key' => 'session_end_time',
                                 'value' => date('Ymd'),
		                 'type' => 'DATE',
		                 'compare' => '<'
                                )
                              ) );
    $query->set( 'no_found_rows', true ); //this removes pagination - delete if pagination needed
  }
}
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' );
?>```

Source for pre-query date ACF Field - https://support.advancedcustomfields.com/forums/topic/using-datepicker-field-in-a-wp_query-date-query/
@hamish-strachan

This comment has been minimized.

Copy link

@hamish-strachan hamish-strachan commented Dec 15, 2020

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

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Dec 15, 2020

I missed a comma after the first key. Try copying the code again. I have just adjusted it.

@hamish-strachan

This comment has been minimized.

Copy link

@hamish-strachan hamish-strachan commented Dec 15, 2020

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

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Dec 15, 2020

Did you update the CPT_SLUG section?

What format is your date field in?

@hamish-strachan

This comment has been minimized.

Copy link

@hamish-strachan hamish-strachan commented Dec 15, 2020

yeah, I'm guessing that should be 'session'

@hamish-strachan

This comment has been minimized.

Copy link

@hamish-strachan hamish-strachan commented Dec 15, 2020

Is it possibly because 'type' is set to date? My fields use DATETIME.

@hamish-strachan

This comment has been minimized.

Copy link

@hamish-strachan hamish-strachan commented Dec 15, 2020

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?

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Dec 15, 2020

Yes you might need to change the date format and the type to what you have.

@hamish-strachan

This comment has been minimized.

Copy link

@hamish-strachan hamish-strachan commented Dec 15, 2020

Already tried and it's still not working :/

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Dec 15, 2020

Do you want to join the Discord channel https://discord.gg/HHaQ7judq5 will be easier to help debug

@hamish-strachan

This comment has been minimized.

Copy link

@hamish-strachan hamish-strachan commented Dec 15, 2020

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...

function dynamic_category_query( $query ) {
  if ( $query->query['post_type'][0] == 'session' ) {   //applies to 'session' post type
     $meta_query_args = array(
                                array(
                                'key' => 'session_start_time',
                                'value' => date('Y-m-d H:i:s'),
		                 		'type' => 'DATETIME',
		                 		'compare' => '<'    
                                ),                                 // session_start_time is in the past
                               array(
                                 'key' => 'session_end_time',
                                 'value' => date('Y-m-d H:i:s'),
		                		 'type' => 'DATETIME',
		               			 'compare' => '>'
                                ),                                 //session_end_time is in the future
      						 array(
         						'key' => 'session_location',
        						 'value' => 'main-auditorium'
                              ) );                                //session_location is 'main-auditorium'
    	$query->set( 'meta_query', $meta_query_args );
    $query->set( 'no_found_rows', true );   //this removes pagination - delete if pagination needed
  }
}
add_action( 'pre_get_posts', 'dynamic_category_query' );
?>```
@rickfitz

This comment has been minimized.

Copy link

@rickfitz rickfitz commented Jan 3, 2021

I've been struggling with something very similar, and it's now working thanks to this thread :)
But - I'm now wondering if it's more efficient to filter this way, or to fetch everything and filter using Oxy conditions within the repeater instead. I think I read somewhere that it's often better to use simple database queries and filter with php instead.
I'd be very interested to hear any opinions on that!

@Mediteranija

This comment has been minimized.

Copy link

@Mediteranija Mediteranija commented Jan 11, 2021

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.

@Mediteranija

This comment has been minimized.

Copy link

@Mediteranija Mediteranija commented Jan 15, 2021

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' );
?>
@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Jan 15, 2021

@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"

@Mediteranija

This comment has been minimized.

Copy link

@Mediteranija Mediteranija commented Jan 15, 2021

@tdrayson Yes, I'm aware of that. But I wanted to tweak the repeater itself to output only current user's posts (if any).

@Wpaid

This comment has been minimized.

Copy link

@Wpaid Wpaid commented Jan 18, 2021

Hi @tdrayson

Pleas help me.
How can I link the code to oxygen correctly?
I placed the code correctly! In two "Code block".
I made an "Image" and a "Heading text" that I would like to appear in the Sidebar.
What should I use in the "Insert Data" part, to appear the correct text. And what should I use in the "Insert Dinamic Data" section to appear the correct image.
If I select "Featured Image", and "Title" in the "Dinamik data" the image and text will be displayed, but only sorted by date, so the code will not work.
Thank you in advance.

Screenshot oxygen

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Jan 18, 2021

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?

@Wpaid

This comment has been minimized.

Copy link

@Wpaid Wpaid commented Jan 18, 2021

@tdrayson
I want to make a Related posts by category, and I want the order is random by category.
But it now show me the posts by date.
The latest post is in top in the list.
In the "Dynamic data", the featured image and the "Title"is a good choice?

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Jan 18, 2021

@Wpaid
you have the repeater set to custom and you have the post type and count set and nothing else?

@Wpaid

This comment has been minimized.

Copy link

@Wpaid Wpaid commented Jan 19, 2021

@tdrayson
Yes. That's all I set. Query -> Custom-> Post type -> post, and "Post per page" -> 5.
I'm sure I'm going to spoil something. I'm just looking for the right setting.
I pretty much understand what the code does, but something is still missing.
Is there a special setting I can't figure out?
I am not change nothing in your code.
The repeater working well. But when I put the code to the "Code block" there is doing nothing.
With code and without code, same result.
This is the Structure
Screenshot Oxygen1

@tdrayson

This comment has been minimized.

Copy link

@tdrayson tdrayson commented Jan 20, 2021

Did you update the code so you changed the taxonomy slug and changed the post type slug?

@GC-DK

This comment has been minimized.

Copy link

@GC-DK GC-DK commented Jan 20, 2021

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!

@Wpaid

This comment has been minimized.

Copy link

@Wpaid Wpaid commented Jan 20, 2021

Did you update the code so you changed the taxonomy slug and changed the post type slug?
@tdrayson
I did it. Now good. Working perfect. The category slug was not well filled.
Thank you again.

@GC-DK

This comment has been minimized.

Copy link

@GC-DK GC-DK commented Jan 21, 2021

I'm still having issues with this.
Can anyone confirm if they have tried this in v.3.7 and it works?

@williammei

This comment has been minimized.

Copy link

@williammei williammei commented Jan 22, 2021

Nested inside another repeater is not working, I used toolset relationship to connect two posts.
Could you help on this ?

@Jehu

This comment has been minimized.

Copy link

@Jehu Jehu commented Jan 28, 2021

I'm still having issues with this.
Can anyone confirm if they have tried this in v.3.7 and it works?

Yes, here it works with 3.7!

@Kuldas

This comment has been minimized.

Copy link

@Kuldas Kuldas commented Feb 9, 2021

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.

@mindingear

This comment has been minimized.

Copy link

@mindingear mindingear commented Feb 12, 2021

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,

@mindingear

This comment has been minimized.

Copy link

@mindingear mindingear commented Feb 12, 2021

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.

@albertvisuals

This comment has been minimized.

Copy link

@albertvisuals albertvisuals commented Mar 14, 2021

Hello!
Thanks for sharing this, that is really helpful!

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:
post_type=veranstaltungen&posts_per_page=4&custom_query=true

I then have following in a code snippet:

`function dynamic_category_query999( $query ) {

if ( $query->get( 'custom_query' ) ) {

    $query->set( 'meta_key', 'veranstaltung_zeit_anfangszeitpunkt' );
    $query->set( 'orderby', 'meta_value' );
    $query->set( 'order', 'DESC' );
    $query->set(
        'meta_query',
        [
            [
                'key'     => 'veranstaltung_zeit_endzeitpunkt',
                'value'   => gmdate( 'Y-m-d H:i:s' ),
                'compare' => '<=',
            ],
        ]
    );

}

}
add_action( 'pre_get_posts', 'dynamic_category_query999' );`

The question I now have:
Is it possible to add a speicifc identifier like queryA, queryB, queryC etc. so that I cann apply a specific code snippet to only one specific repeater?

Thanks very much again!

@ilhamhady

This comment has been minimized.

Copy link

@ilhamhady ilhamhady commented Mar 15, 2021

Hi @ALL, how if I want to include all categories there?

@mark88008

This comment has been minimized.

Copy link

@mark88008 mark88008 commented Mar 18, 2021

Thank you very much. It works very well for me!

@Blikeriker

This comment has been minimized.

Copy link

@Blikeriker Blikeriker commented Mar 21, 2021

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!

That would be amazing!! :(

@ilhamhady

This comment has been minimized.

Copy link

@ilhamhady ilhamhady commented Mar 23, 2021

I heard this feature will release on Oxygen 3.8, Amazing!

@Blikeriker

This comment has been minimized.

Copy link

@Blikeriker Blikeriker commented Mar 30, 2021

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!

I have solved this ....contact me if you still need to know how to do it (so we not troll this post...) -> skp: live:indirect_9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment