Support

Account

Forum Replies Created

  • Sorry, I realised I must have posted that up in haste… here is the complete version with the permalink echo:

                            <?php
                            $selected = get_field('film_filmreview');
                            $selected = array_pop($selected); ?>
    
                            <?php echo $selected;
                            $permalink = the_permalink($selected->ID); ?>
    
                        </div>
                        <h1 style="word-break: break-word;"><?php echo $selected ?>
                            <?php $permalink = the_permalink($selected->ID); ?></h1>
  • Hi Jarvis,

    Hope you’re well?
    Unfortunately since implementing this, I’ve found adding a redirect to the Homepage for users when they login breaks the films filters and actually inputs part of the page they’re redirected to in place of the filtered data.

    I don’t know enough about Ajax at this stage to work out what the problem is exactly but it’s like the page is working like an iframe and trying to place the Homepage content into the “filtered-films” element.

    // Redirect subscriber accounts to homepage
    add_action('admin_init', 'redirectSubscribersToHomepage');
    
    function redirectSubscribersToHomepage()
    {
    	$currentUser = wp_get_current_user();
    
    	if (count($currentUser->roles) == 1 AND $currentUser->roles[0] == 'subscriber') {
    		wp_redirect(site_url('/'));
    		exit;
    	}
    }

    https://pasteboard.co/GoO2kKmKWfoo.png

    Any thoughts please?

  • Thanks John! I’ve now switched the Return Format to a custom value of “Y-m-d”.
    I’ve also updated the dateToday variable set for the comparison to match the same format.

  • Thanks John! I ended up formatting the date using the following code since I’m using the field to run several if statements inline between today’s date and the field results elsewhere on the page to show/hide content.

    <?php echo date("d/m/Y", strtotime(get_field('film_releasedate'))); ?>

    Please let me know if you have any further advice though – much appreciated!

  • Ah ok thanks for the explanation! I’ve now changed the Return Format to Ymd to make the comparison and any if statements further down the page easier to work with. I’ve also removed the following line from all queries.

    'type' => 'DATE',

    My next task is to simply re-format the date in terms of how it’s displayed as it’s just using Ymd for the moment. I presume I can use something like the following (haven’t got time to test at the moment until later today)?

    $date = get_field('date');
    $date2 = date("F j, Y", strtotime($date));

    https://apexcinemas.andrewcourtney.co.uk/coming-soon/
    https://apexcinemas.andrewcourtney.co.uk/films/no-time-to-die-2/

    Thanks in advance!

  • Ok, so it would appear this was due to the Return Format I had setup prior to the query being run.

    I’m still having problems understanding the compare, especially when the type is set to “DATE”, however.

    If I set the Return Format for the ACF to “Ymd” then it breaks the release date set on the individual films, whereas if I use the Custom value of “U” and run my query then it displays correctly (I then have to change the formatting later so that it displays in a “d/m/Y” format. For example, one film changes the set release date from 21/07/2028 to 23/08/1970?

        $dateToday = date('Ymd');
    
        $comingSoonFilms = new WP_Query(array(
            'post_type' => 'film',
            'posts_per_page' => -1,
            'order' => 'ASC',
            'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key' => 'film_releasedate',
                    'value' => $dateToday,
                    'type' => 'DATE',
                    'compare' => '>',
                ),
                array(
                    'key' => 'film_active',
                    'value' => '1',
                ),
            )
        ));
  • Ok, so after some thought I had a feeling that I had to specify that I wanted to have both queries return true for it to display the relevant data. I adjusted my query to include a “relation” but still the incorrect results are showing for Coming Soon, and no results are pulling through for Now Showing.

    https://apexcinemas.andrewcourtney.co.uk/coming-soon/
    https://apexcinemas.andrewcourtney.co.uk/now-showing/

    Below are the amended queries for each page:

    Coming Soon

        $comingSoonFilms = new WP_Query(array(
            'post_type' => 'film',
            'posts_per_page' => -1,
            'order' => 'ASC',
            'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key' => 'film_releasedate',
                    'value' => $dateToday,
                    'type' => 'DATE',
                    'compare' => '>',
                ),
                array(
                    'key' => 'film_active',
                    'value' => '1',
                ),
            )
        ));

    Now Showing

        $nowShowingFilms = new WP_Query(array(
            'post_type' => 'film',
            'posts_per_page' => -1,
            'order' => 'DESC',
            'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key' => 'film_releasedate',
                    'value' => $dateToday,
                    'type' => 'DATE',
                    'compare' => '<',
                ),
                array(
                    'key' => 'film_active',
                    'value' => '1',
                ),
            )
        ));

    If any other details would be helpful let me know!

    Thanks in advance.

  • Ok so on review I think it must be something to do with the conditions within the query I’m running as I’ve double-checked the Return Format of the ACF and it’s set to a Custom value of “U” which should match back to my dateToday variable value.

    $comingSoonFilms = new WP_Query(array(
            'post_type' => 'film',
            'posts_per_page' => -1,
            'order' => 'ASC',
            'meta_query' => array(
                array(
                    'key' => 'film_releasedate',
                    'value' => $dateToday,
                    'type' => 'DATE',
                    'compare' => '>',
                ),
                array(
                    'key' => 'film_active',
                    'value' => '1',
                ),
            )
        ));

    As you can see from the live deployment, I’ve printed both the formatted and unformatted data for the release date. Therefore I don’t think this is related to the Advanced Custom Field itself.

    Any thoughts please or things I could try to debug the issue with?

  • Hi all,

    I copied @tomgreeen’s solution into my functions.php and I get the following errors:

    Any thoughts what may be causing this? Note that I get this error on saving my repeater field entries.

    Many thanks in advance!

  • Hi Jarvis,

    I’ve now started to flesh out the design of the page a bit more by introducing what I had before the filters were put in place which used ACFs to show content such as an image, the relevant film certificate as well as a short writeup and “view more” CTA.

    However, after putting the relevant code within the while loop, I’ve found that within a split second it is replaced by text-only examples.

    Please could you advise?

    Many thanks!

  • Ah excellent – thank you so much for your help! Very much appreciated!

  • Sorry, not sure what happened with my code there – I think the forum misinterpreted my a tag and changed it to a link!

    <!-- START "You may also like" -->
                    <?php
    
                    $filmGenre = get_field('film_genre');
                    // args
                    $args = array(
                        'numberposts'     => -1,                    // Displays ALL post
                        'post_type'     => 'film',    // Change to reflect the name of your custom post type
                        'meta_key'        => 'film_genre',      // Change to reflect the name of your custom field
                        'meta_value'    => $filmGenre,           // Change to reflect the name of the value in your custom field
                        'post__not_in' => array($post->ID) // Exclude current post ID
                    );
                    // get results
                    $the_query = new WP_Query($args);
                    // The Loop
                    ?>
                    <?php if ($the_query->have_posts()) : ?>
                        <div class="genericpage--film__details genericpage--film__details--ymal">
                            <a name="ymal"></a>
                            <h2>You may also like</h2>
                            <!-- START - You May Also Like Slider -->
                            <div class="section__slider-container text-center">
                                <div class="filmpanels swiper swiper-container swiper--section swiper--section--filmsymal">
                                    <div class="swiper-wrapper">
                                        <?php while ($the_query->have_posts()) : $the_query->the_post() ?>
                                            <div class="swiper-slide">
                                                <a href="<?php the_permalink(); ?>" title="">
                                                    <img src="<?php echo wp_get_attachment_url(get_field('film_poster')); ?>" alt="<?php the_title() ?>" title="<?php the_title() ?>" class="swiper__imgbg" />
                                                    <p><?php the_title() ?></p>
                                                </a>
                                            </div>
                                        <?php endwhile; ?>
                                    </div>
                                    <div class="swiper-button-prev"></div>
                                    <div class="swiper-button-next"></div>
                                </div>
                            </div>
                            <!-- END - You May Also Like Slider -->
                        </div>
                    <?php endif; ?>
                    <?php wp_reset_query();  // Restore global post data stomped by the_post().
                    ?>
                    <!-- END - "You may also like" -->
  • Sorry it’s been a few days Jarvis – thank you so much for the clear explanation and putting this together. It definitely gives me a solid foundation to build on!

    A couple of questions I had was that while testing the setup, I noticed that it would never display the message “No films found matching your criteria.” it would seem, as only the genres that are assigned to films are shown on the form – i.e. it doesn’t display ALL genres by default, regardless of whether there are films assigned or not? Just an observation though as really you wouldn’t want someone to select an option if no results were available.

    Also, on the single film page I had the genre being displayed via a simple “get_field” for the ACF “film_genre” contents, but also a “You May Also Like” section based entirely on films with the same genre – again, using the “film_genre” contents as a basis.

    <!– START “You may also like” –>
    <?php

    $filmGenre = get_field(‘film_genre’);
    // args
    $args = array(
    ‘numberposts’ => -1, // Displays ALL post
    ‘post_type’ => ‘film’, // Change to reflect the name of your custom post type
    ‘meta_key’ => ‘film_genre’, // Change to reflect the name of your custom field
    ‘meta_value’ => $filmGenre, // Change to reflect the name of the value in your custom field
    ‘post__not_in’ => array($post->ID) // Exclude current post ID
    );
    // get results
    $the_query = new WP_Query($args);
    // The Loop
    ?>
    <?php if ($the_query->have_posts()) : ?>
    <div class=”genericpage–film__details genericpage–film__details–ymal”>

    <h2>You may also like</h2>
    <!– START – You May Also Like Slider –>
    <div class=”section__slider-container text-center”>
    <div class=”filmpanels swiper swiper-container swiper–section swiper–section–filmsymal”>
    <div class=”swiper-wrapper”>
    <?php while ($the_query->have_posts()) : $the_query->the_post() ?>
    <div class=”swiper-slide”>
    ” title=””>
    ” alt=”<?php the_title() ?>” title=”<?php the_title() ?>” class=”swiper__imgbg” />
    <p><?php the_title() ?></p>

    </div>
    <?php endwhile; ?>
    </div>
    <div class=”swiper-button-prev”></div>
    <div class=”swiper-button-next”></div>
    </div>
    </div>
    <!– END – You May Also Like Slider –>
    </div>
    <?php endif; ?>
    <?php wp_reset_query(); // Restore global post data stomped by the_post().
    ?>
    <!– END – “You may also like” –>

    Thanks again; plenty for me to think about!

  • Yes that’s exactly what I meant but I couldn’t find how to create a custom taxonomy at the time.

    However, after a bit more research I found that I can install this plugin and create a custom taxonomy that way – would that be the best approach?

    https://en-ca.wordpress.org/plugins/custom-post-type-ui/

  • Ok so I’ve thought about this a bit more and experimented with the taxonomy field type but quickly realised that I’d need a separate taxonomy from for example the default posts (e.g. tags or categories). I’m currently using the default posts for a blog part of the website so I don’t want to cross-pollinate between my custom post type and the default posts.

    I’ve tried looking into hiding the option on the default posts menu but all of the solutions result in errors in parts of the sidebar menu.

    Anyway – I resorted to going back to the tutorial Elliot created but I’m now getting an error with my foreach for some reason, no matter whether I’m viewing the standard view or the filtered view?

    https://apexcinemas.andrewcourtney.co.uk/films/
    https://apexcinemas.andrewcourtney.co.uk/films/?film_genre=Action

    Tutorial link again: https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    <div id=”search-filmgenre”>
    <?php

    $field = get_field_object(‘film_genre’);
    $values = explode(‘,’, $_GET[‘film_genre’]);
    $filmGenres = $field[‘choices’];

    ?>

      <?php foreach($filmGenres as $choice_value => $choice_label) : ?>

    • <input type=”checkbox” value=”<?php echo $choice_value; ?>” <?php if (in_array($choice_value, $values)) : ?>checked=”checked” <?php endif; ?> /><?php echo $choice_label; ?>
    • <?php endforeach; ?>

    </div>

    Any thoughts please? Thanks so much for your help so far.

  • Wow ok I’ll give that a go – thanks!
    Just one question; as I have the single-film.php page template, I am displaying the film genre there too as a simple get_field. If I change the film_genre ACF to taxonomy will this part need to be redone?

    See the left column on the following page for an example:
    https://apexcinemas.andrewcourtney.co.uk/films/no-time-to-die/

  • Unfortunately it is returning the same template for both the standard and the results page – see the URL shown at the bottom.

    https://apexcinemas.andrewcourtney.co.uk/films/
    https://apexcinemas.andrewcourtney.co.uk/films/?film_genre=Action

    By the way, if it helps, below are the post type details I entered for “film”/”films” – may be useful?

    // Film Post Type
    register_post_type(‘film’, array(
    ‘show_in_rest’ => true,
    ‘supports’ => array(‘title’, ‘excerpt’),
    ‘rewrite’ => array(‘slug’ => ‘films’),
    ‘has_archive’ => true,
    ‘public’ => true,
    ‘labels’ => array(
    ‘name’ => ‘Films’,
    ‘add_new_item’ => “Add New Film”,
    ‘edit_item’ => “Edit Film”,
    ‘all_items’ => “View All Films”,
    ‘singular_name’ => ‘Film’
    ),
    ‘menu_icon’ => ‘dashicons-editor-video’
    ));

  • Ok so I’ve managed to get my images pulling through even when filtering which was a simple fix of changing the Return Value of the “film_poster” field to Image ID. It now works when I’m viewing the standard /films page as well as if I send a query (e.g. /films/?film_genre=Action).

    However, I’m still having problems with the filters disappearing when the query is sent (e.g. apexcinemas.andrewcourtney.co.uk/films/?film_genre=Action) – any ideas?

    Below is the code I have running in the functions.php page, taken from the ACF tutorial I mentioned before 🙂

    // filter film genre
    add_action(‘pre_get_posts’, ‘my_pre_get_posts’);

    function my_pre_get_posts($query){
    // validate
    if(is_admin()){
    return;
    }

    // Get original meta query
    $meta_query[] = $query->get(‘meta_query’);

    // allow the url to alter the query
    // e.g. ?film_genre=comedy
    if(isset($_GET[‘film_genre’])){

    $film_genre = explode(‘,’, $_GET[‘film_genre’]);

    // Add our meta query to the original meta queries
    $meta_query[] = array(
    ‘key’ => ‘film_genre’,
    ‘value’ => $_GET[‘film_genre’],
    ‘compare’ => ‘IN’,
    );
    }

    // update the meta query arguments
    $query->set(‘meta_query’, $meta_query);

    // always return
    return;
    }

    Thanks in advance!

  • Sorry, not quite sure why my replies were duplicated so much – the forum only seemed to take one reply but posted multiple versions.

    Please refer to the latest reply directly above this one, posted November 20, 2021 at 9:25 pm.

    Many thanks!

  • If I change the return format of “film_poster” to Image ID, presumably that will break all other instances of the image unless I set wp_get_attachment_url() on them?

    The ACF field is assigned to the “Film” Field Group which in turn is set to only appear when the Post Type equals “Film” (which has a slug rewrite set on it to change “film” to “films”).

    What I really don’t get is that the images appear when you run a query, whereas the standard page shows no images. And, when viewing the standard page, the filters are displayed. However if you run a query the numbers disappear.

    If you need any further details to look into this please let me know.

  • If you check the docs for wp_get_attachment_url()
    
    You will see it accepts an attachment ID but your ACF field is returning:
    
    Uses “Image URL” Return Format
    
    So this may explain why the images don’t work

    Ah I had no idea to be honest (haven’t been working with WordPress long). Presumably if I change the “film_poster” field to return the Image ID, I’ll need to change all instances where I’m using that field so that it uses wp_get_attachment_url?

    I find it strange that the standard page doesn’t load any of the images, while the query version of the same page pulls the images through correctly?

    So where have you assigned that ACF field to?

    The “film_genre” field is attached to the “Film” Field Group, which in turn only shows if the Post Type “is equal to” “Film”. The “Film” Post Type has a slug rewrite on it of “films”, hence the URL.

    Let me know if any other details would be helpful to find out what’s wrong.

  • If you check the docs for wp_get_attachment_url()

    You will see it accepts an attachment ID but your ACF field is returning:

    Uses “Image URL” Return Format

    So this may explain why the images don’t work`

    Ah I had no idea to be honest (haven’t been working with WordPress long). Presumably if I change the “film_poster” field to return the Image ID, I’ll need to change all instances where I’m using that field so that it uses wp_get_attachment_url?

    I find it strange that the standard page doesn’t load any of the images, while the query version of the same page pulls the images through correctly?

    So where have you assigned that ACF field to?

    The “film_genre” field is attached to the “Film” Field Group, which in turn only shows if the Post Type “is equal to” “Film”. The “Film” Post Type has a slug rewrite on it of “films”, hence the URL.

    Let me know if any other details would be helpful to find out what’s wrong.

  • If you check the docs for wp_get_attachment_url()

    You will see it accepts an attachment ID but your ACF field is returning:

    Uses “Image URL” Return Format

    So this may explain why the images don’t work

    Ah I had no idea to be honest (haven’t been working with WordPress long). Presumably if I change the “film_poster” field to return the Image ID, I’ll need to change all instances where I’m using that field so that it uses wp_get_attachment_url?

    I find it strange that the standard page doesn’t load any of the images, while the query version of the same page pulls the images through correctly?

    So where have you assigned that ACF field to?

    The “film_genre” field is attached to the “Film” Field Group, which in turn only shows if the Post Type “is equal to” “Film”. The “Film” Post Type has a slug rewrite on it of “films”, hence the URL.

    Let me know if any other details would be helpful to find out what’s wrong.

Viewing 25 posts - 1 through 25 (of 26 total)