Support

Account

Home Forums Front-end Issues Multiple array for sub field within meta_query not working correctly

Solved

Multiple array for sub field within meta_query not working correctly

  • Hi there, I have been seeking help through support, but not really getting anywhere sadly. I’m not knocking support by the way, I know they are busy and do a good job. I was wondering if anyone can see what I’m doing wrong here.

    We have a custom post type called Location. That CPT has ACF assigned to it. Specifically, we have a repeater field with a sub set of fields. These are essentially created for different files, like PDFs. Some Locations might have one, or a few. For each file, we have a checkbox (true/false), to mark it as ‘archived’. This doesn’t need to be anything special, other than, we want to then sort any files (those individual sub fields of the repeater field) in the template file so that a website user can view those files, and any files marked as archived should be in an archived section of that page.

    So I’m running two queries, both separate loops. One showing all files not marked as archived (false), and the other showing files as archived (true). Here’s the code, as an example, for the archived files. The first array in the meta_query is working, where it checks whether the user logged in is also set within the ACF on that post type. The second array, which checks if that post is marked as archived, does not work.

    To confirm, I want to check that the user logged in is assigned to that custom post via the appropriate ACF field, and I also want to check that this post is marked as archived using another ACF.

    Am I using meta_query and relation wrong here?

    Ps, I am happy to seek professional support. I have paid for ACF PRO but appreciate $100 or whatever it is shouldn’t afford me lifetime support with instant access. If anyone can recommend any specialists in ACF that’d be great (I did try to find @hube2‘s contact but it looks like he’s taken the contact form down on his site).

    Thanks

    <?php
    					//filter
    					function my_posts_where( $where ) {
    						$where = str_replace("meta_key = 'content_reports_$", "meta_key LIKE 'content_reports_%", $where);
    						return $where;
    					}
    					
    					add_filter('posts_where', 'my_posts_where');
    				
    					//args
    					$archive_args = array(
    						'numberposts' 	=> -1,
    						'post_type' 	=> 'location',
    						'meta_query'	=> array(
    							'relation'	=> 'AND',
    								array(
    								'key'		=> 'location_assign_user',
    								'value'		=> '"'.$current_user->ID.'"',
    								'compare'	=> 'LIKE'
    								),
    								array(
    								'key'		=> 'content_reports_$_content_reports_archive',
    								'compare' 	=> '=',
    								'value'		=> '1'
    								)
    						)
    					);
    					
    					//query
    					$the_archive_query = new WP_Query($archive_args);
    					
    					?>
  • I’ve reworked this and removed the array for checking if it is marked as an archive, instead, I’ve included an if statement within the loop to check whether each repeater has archive sub_group marked true or false. This has helped me achieve the results I was after.

    Makes more sense that way, though probably not as quick.

    Thanks

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.