Support

Account

Home Forums General Issues Loop issue with checkbox

Solved

Loop issue with checkbox

  • Hi there. I’m pretty new to WordPress and the last couple of weeks I’ve been tinkering on a website. Ever since discovering ACF (great plug-in!) I’m trying to make the back-end more and more advanced, trying to convert lots of shady loops that used category’s to custom fields instead.

    But I’ve come across a problem, which just ended up in a lot of frustration for a good few days, but finally after thinking logically and testing out a lot of things I find out what is causing the problem, but I have no idea how to fix it.

    So the problem is I have a loop, and the purpose of this loop is to showcase 1 Featured news item. Previously I handled this by adding an additional category called ‘featured’ resulting in news items having multiple categories… and with also an image slider handled like this, some news items could end up having 2 or 3 categories, which isn’t really practical. So I wanted to convert this into a checkbox, which is more user friendly and use the categories where they are suppose to be used for, sounds decent right?

    The code:

    <?php
         $args = array (
              'post_type' => 'nieuws',
              'posts_per_page' => 1,
              'orderby' => 'date',
              'order' => 'DESC'
         );
    $nieuws = new WP_Query($args);
    if ( $nieuws->have_posts() ) : ?>
         <?php while ( $nieuws->have_posts() ) : $nieuws->the_post();?>
              <?php if( in_array( 'featured_spot', get_field('featured') ) ) { ?>
    
    	<!--article layout goes here-->
    
              <?php } ?> 
         <?php endwhile; ?>
    <?php endif; ?>
    <?php wp_reset_query(); // reset the query ?>

    So what I think this does is first loop through all the posts of the post type nieuws and then check whether or not it has the featured checkbox ticked and then post the latest by date in the list.

    So for testing purposes I created 4 news posts, let’s call them A B C D, with posts_per_page set to 1, orderby to date and order to DESC, meaning A is the oldest, and D is the newest post. I’ve then ticked the featured checkbox on all 4 news posts. With the code above it resulted in showing the latest post ( post D ) in the featured area, just like expected, no problems there.

    The problem arises when I untick the featured checkbox of post D, by logic you would expect the loop to go to the next post that has the checkbox ticked, but sadly it didn’t, resulting in an empty space having no output. I tested things by setting the post per page to 2, which resulted in having the second latest ( post C ) to show up. So the problem is that the loop doesn’t skip over a news item if it doesn’t have the featured checkbox ticked.

    Being the designer I am, I’m already happy to have found the reason behind the problem, unfortunately I don’t have the programming experience to come up with a solution. So I hope one of you folks out there could help me out with this problem. Thank you in advance!

  • Hi,

    in your example you’re checking the whole array of “featured”. Well, you’re already in the loop, so you’re already just working with a single post – and that post can either have the “featured” field on or off.

    If you have the ‘featured’ field as a boolean (true/false), you can just edit your IF line to this:

    <?php if(get_field('featured')) { ?>

    And if you’re setting this field to something custom, just check it against that value:

    <?php if(get_field('featured') == 'custom_value') { ?>

    … ad as far as getting only one post (last) – fix your query, you can set “posts_per_page” to -1 to include all of the featured posts.

  • Thank you for your reply, unfortunately with this info i could’t fix the problem. I understand that I’m already in the loop so it works just with the first post. But the situation where i’d like to get to is if the first post doesn’t have the checkbox ‘featured’ activated, it skips to the next post down the line to see if it’s activated there and then output the result. I know of the posts_per_page -1 showing all posts, but i want the latest possible post that has the checkbox featured activated ( only show 1 post). So i guess i need some form of check included in the loop to check for the ‘featured’-value of the checkbox and if a post doesnt have that value it moves to the next post to see if it has and output that post instead.

  • Ok, so you want to only display the latest featured post, right?

    You need to specify everything in the WP_Query – there’s no need for additional checking, if you only want one post. Use the meta_query, like this:

    $args = array (
    	'post_type' => 'nieuws',
    	'posts_per_page' => 1,
    	'orderby' => 'date',
    	'order' => 'DESC',
    	'meta_query' => array(
    	     array(
    	        'key'		=> 'published',
    	        'compare'	=> '=',
    	        'value'		=> 1
    	    )
        ),
    );

    You can find more examples of such queries here: http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/

  • And I think this simpler example would also work (replace the name of the custom field for featured in both examples):

    $args = array (
    	'post_type' => 'nieuws',
    	'posts_per_page' => 1,
    	'orderby' => 'date',
    	'order' => 'DESC',
    	
    	'meta_key' => 'custom_field_featured',
    	'meta_value' => 1,
    	'meta_compare' => '='
    );

    Veel succes 😉

  • That way of putting it in the query from the start is the first thing i tried to tackle this problem when it arose.

    $args = array (
        'post_type' => 'nieuws',
        'posts_per_page' => -1,
        'orderby' => 'date',
        'order' => 'DESC',
        'meta_key' => 'featured',
        'meta_compare' => '=',
        'meta_value' => 'featured'    
    );

    I tried changing the meta_value to 1 like in your example, I tried to change the values for the checkbox itself in the admin panel, but some how it just doesn’t want to show any post, even if i set the posts_per_page to -1 just for testing purposes. And this got me looking to other solutions like in my first post. I know there is something wrong with the meta_value bit of the query, I tested if the meta_key on its own works and it does. The solution may well be in a small corner, but I just can’t figure it out hehe…

  • Oke nevermind, I finally got it to work, like i said, solutions often are in a small corner.

    'post_type' => 'nieuws',
                        'posts_per_page' => 1,
                        'orderby' => 'date',
                        'order' => 'DESC',
                        'meta_query' => array(
                             array(
                                'key'       => 'featured',
                                'compare'   => 'LIKE',
                                'value'     => 1
                            )
                        )

    I changed the compare from ‘=’ to ‘LIKE’. I don’t understand the reason why ‘=’ doesn’t work and ‘LIKE’ does, but atleast it solved the problem!

    Thanks for the support Enorog!

    Fijne dag verder 🙂

  • Hi,
    I have the same situation but with multiple checkbox,

    select_section = is where the post will be displayed (fine with that)
    pin_article = is a checkbox with few options (project is one of them), when is checked this post should be pinned on project page.

    Problem is making the pinned post to be displayd first on project page.
    work fine with single checkbox but with multiple values is not working anymore.

    Any solution to that?

    $query_args = array(
                'numberposts' => -1,
                'posts_per_page' => 4,  
                'post_type' => 'project_post',
                'meta_key' => 'pin_article',  
                'orderby' => 'meta_value',
                'meta_query'  => array(
                'relation'    => 'OR',
                  array(
                    'key'   => 'select_section',
                    'value'   => 'home',
                    'compare' => 'LIKE'   
                  ),
                  array(
                    'key'   => 'pin_article',
                    'value'   => 'project',
                    'compare' => 'LIKE'
                  )
                ));
Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.