Support

Account

Home Forums General Issues WP loop via checkbox value

Solving

WP loop via checkbox value

  • Sorry, I have searched extensively and found nothing that worked that I understood.

    Simple custom post type loop, want to check for a selected checkbox value, if it is true then that item is included in the loop.

    Can anyone help?

    Thanks.

  • Can anyone offer any type of guide? I’ve tried half a dozen or more “solutions” found online, including code examples from ACF docs, and nothing is working. Surely someone must have done this??

  • while(){
    
    if( in_array( 'red', get_field('field_name_of_checkbox') ) )
    {
        //...Your code
    }
    
    }

    Be careful though because if there is nothing checked I think the field returns a string. This is off the top of my head. Not sure.

  • Haven’t tried this yet, but the problem with this one I think is that the check isn’t in the loop, so it would simply skip an item rather than not include it at all, so you couldn’t output a particular number. Nor could you use pagination.

    Cheers.

  • what do you mean the check isn’t in the loop? In my code above the if is wrapped in a while loop? Just replace with the syntax you are using for The Loop?

  • You have a checkbox field, by this I’m assuming that this is a True/False field because you say "if it is true then that item is included in the loop"

    When you say that you want it included in the loop, here I’m assuming that you mean included in the posts that are returned by WP_Query, either the main query or a custom query.

    Is it a custom query or the main WP query that you are looking to alter to only include posts that are marked as true? My answer below is assuming that you mean the main WP query because you did not mention a custom query and "if it is true then that item is included in the loop"

    Let me know if I have any of this wrong.

    If my assumptions are correct then you need to add a pre_get_posts filter in WP.

    To your functions.php file add something like:

    
    // change "my_post_type" to the name of your post type
    // change "my_field" to the name of your ACF field
    function my_post_type_pre_get_posts($query) {
      if (is_admin() || !$query->is_main_query()) {
        return;
      }
      if ($query->query_vars['post_type'] == 'my_post_type') {
        $meta_query = array(
          array(
            'key' => 'my_field',
            'value' => '1' // this is the value that ACF saves
                           // for a true value in a true/false field
          )
        );
        $query->set('meta_query', $meta_query);
      }
    }
    add_action('pre_get_posts', 'my_post_type_pre_get_posts');
    
  • Your initial assumption is correct, I need it to filter out the posts that are returned so needs to be in the query. Not for the main loop, but for a Custom Post Type loop.

    But it’s a checkbox, so it can have one or multiple items. Though I am checking for just one item per query (this will be used a number of times depending on page) so it might still work, I’ll give it a go.

    Cheers.

  • Though… not really understanding why this is in the functions, and how I actually display this in a template page…?

  • This – from the ACF docs – is the kind of thing I’m trying to do, but this code isn’t working:

    $posts = get_posts(array(
        'meta_query' => array(
            array(
                'key' => 'field_name', // name of custom field
                'value' => '"red"', // matches exaclty "red", not just red. This prevents a match for "acquired"
                'compare' => 'LIKE'
            )
        )
    ));
    
    if( $posts )
    {
        //...
    }
  • First sn explanation of why I say to use pre_get_posts and to put the in functions.php.

    If you have a custom post type, and you create a template for the archive of that post type, then you do not need to do a query to get the posts of the post type, WP does that already.

    Example: I create a post type called “product” and I create 2 template files “single-product.php” and “archive-product.php”. WordPress automatically does the queries needed to get the product posts when these templates are use and these templates will be used whenever product posts are being requested. WP will run whether you want it to run or not so you may as well use it. The pre_get_posts hook allows you to alter what WP gets in this default query.

    Doing this any other way doubles the amount of queries and work that WP needs to do to load a page.

    If you still want to do a query then you can add the meta query I gave you to the query you are doing.

    
    $args = array(
        'post_type' => 'my-post-type',
        'posts_per_page' => 6,
        'meta_query' => array(
            array(
                'key' => 'my_field',
                'value' => '1'
            ),
    );
    $query = new WP_Query($args);
    
Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘WP loop via checkbox value’ is closed to new replies.