Lopping over and returning pages with ACF checkbox selected

Hi,

I was looking for some help with implementing a looper provider I am working on. Let’s say I have a select group of pages I want to loop over and return their titles and permalinks. What I’ve done to isolate those pages is I’ve created an ACF which displays a checkbox on all pages asking if this is a “Location Parent” (field name is “location_parent_check”). If that box is checked, I want that page’s title and permalink for each page that meets that condition.

So if there are 5 pages where I’ve checked that box, I want toe looper to go grab all 5 of the titles and all 5 of their permalinks. Ultimately, I’m going to make each of them a button so I can provide links to each location’s landing page. Pretty simple Idea I’m having trouble getting off the ground today.

How can I make that happen with a provider? I feel like I’m missing something really simple that I just can’t see in the documentation.

Thank you in advance for any help with this.

Hi @simeoned,

Unfortunately, the website URL is not accessible and showing the ERR_CONNECTION_TIMED_OUT error. I would suggest you check once and let us know once it is ready to access.

Thanks

https://onward.11massmedia.net/sandbox/ may have had a hiccup at the moment you were trying to access it but it is up and running. Please try again.

Thanks

Hello @simeoned,

With what you have in mind, you will need the “Query String” as your Looper Provider and get the pages where the custom field key is “location_parent_check” and the custom field value is “True” or “Yes”.

Screen Shot 2021-09-02 at 11.50.07 AM

You can check out more about the WP Query strings here:

Kindly let us know how it goes.

@ruenel thank you for clarifying which Looper Provider type I can use for this. However, I am still looking for some direction in how to set up the query itself. I have tried a number of things to get this to work. Including:

post_type=page&posts_per_page=1000&meta_key=location_parent_check&meta_value=yes
post_type=page&posts_per_page=1000&key=location_parent_check&value=yes

Don’t I need something like this:

post_type=page&posts_per_page=1000&{{dc:acf: [NEED_SOMETHING_HERE_TO_GRAB_THE_THING] }}
or
post_type=page&posts_per_page=1000&{{dc:acf: [KEY=location_parent_check] [VALUE=Yes] }}

If you could provide a little more direction here I think that would be really helpful.

Hello @simeoned,

You can follow @Kory’s, (one of our developers), suggestion in getting the query string from this thread:

To get you started in finding out the query string, you will have to write out a query in your child theme’s functions.php using the standard WP_Query syntax like:

$query = array(
	'post_type'              =>  'page',
	'posts_per_archive_page' => '99',
	'meta_query'             => array(
		array(
			'key'     => 'location_parent_check',
			'value'   => 'yes',
			'compare' => 'LIKE',
		),
	),
);

Then, check the output that somehow utilizing http_build_query() , which will format our string accordingly:

var_dump( http_build_query( $query ) );

You may get something like this for the query string:
post_type=page&posts_per_archive_page=99&meta_query%5B0%5D%5Bkey%5D=location_parent_check&meta_query%5B0%5D%5Bvalue%5D=yes&meta_query%5B0%5D%5Bcompare%5D=LIKE

Hope this helps. Best Regards.

@ruenel,

Thank you for the thorough answer and guidance on this. For future reference, I used the ACF documentation found here (https://www.advancedcustomfields.com/resources/checkbox/) to help build my long form query.

The missing step I needed was the http_build_query() function so I could get the URL-encoded query string – something I would never have known how to write from scratch because of the nested array. This info will be very helpful in future projects I am sure. I hope this thread will help others too.

So fo those who are wondering, I wound up outputting 2 var dumps so I could get my head around the second, encoded version of it. I also prettied it up using pre tags. I simply placed this in a php template file I placed in my child theme to read the output:

  $query = array(
    'post_type' => 'page',
    'posts_per_page' => '-1',
    'meta_query' => array(
      array(
        'key' => 'location_parent_check',
        'value' => 'yes',
        'compare' => 'LIKE',
      ),
    ),
  );

echo '<pre>';
  var_dump( $query );
echo '</pre>';

echo '<pre>';
  var_dump( http_build_query( $query ) );
echo '</pre>';

I wonder if this should be considered as something that should be included in your documentation to help others who may not know the build query function is what they need. Might be worth writing up this sort of use case to ensure others can benefit from the general principle.

Thanks again for your support

1 Like

Hey @simeoned,

Thanks for sharing and I’ll submit your feedback as a documentation request.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.