Home › Forums › General Issues › Generating WP archive if an Image field is used
Hi there; could use some pointers, please. I’ve got a WordPress site that shows comic strips. Each page has an English comic at the top, and I’m using ACF Image fields to include translated versions underneath, if available. For example, the fields are called spanish_strip and japanese_strip, etc.
I’d like to create an archive page that includes records where the spanish_strip field has been used, or where japanese_strip field has an entry, so users could look up “spanish strips” and find all those collected together, or “japanese strips” and find those on one page.
I think it could be done via something like this:
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_key' => 'spanish_strip',
'meta_value' => '?????'
));
I don’t know how to pull posts where the value of spanish_strip is basically just “not empty”, since the ID will be different for every image. I have the field set to return
an image ID.
Could anyone suggest a way to handle this?
Hey aloeroot,
I’m guessing you could create a new page template to pull all the posts that feature the ACF fields you’re looking for. This is how I would do it:
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'spanish_strip', // or any other language
'value' => '"'. get_the_ID() .'"',
'compare' => 'LIKE',
)
)
);
$spanish_strips = get_posts($args);
echo '<h1>Archive for Spanish Comic Strips:</h1>';
foreach ($spanish_strips as $strip) : ?>
<!-- You do something nice here :) -->
<?php endforeach;
Note that the get_posts()
function only retrieves the last 5 entries, so either add 'numberposts' => -1
to the arguments or use a standard WP_Query.
Hope this helps!
See the section on this page about Dynamic $_GET parameters
This is the direction I would go in. This would allow altering the existing archive page to only show pages where the alternate language field has a value
meta_key => your field key
meta_value => ”
meta_compare => ‘!=’
Just so there’s a working solution posted for whomever might need it, here’s what I came up with. Thanks for your help.
$striptype = sanitize_text_field( get_query_var( 'striptype' ) ); // calls the striptype from the URL and sanitizes the input so we can have one archive page that brings up archives in different languages
$posts = get_posts(array(
'posts_per_page' => 25,
'post_type' => 'post',
'orderby' => 'date',
'order' => 'ASC',
'meta_query' => array (
array (
'key' => $striptype, //this queries the metadata on the strips, in this case the advanced custom fields, looking for any that are not empty
'value' => '',
'compare' => '!=',
),
),
));
and then further down to actually display the retrieved images there’s one of these code blocks for every language:
if ($striptype == 'spanish_strip') {
$image = get_field('spanish_strip');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
//echo "<strong>Spanish Strip:</strong><br>";
echo wp_get_attachment_image( $image, $size );
echo "<br><br>";
}
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.