Support

Account

Home Forums General Issues WP loop via checkbox value Reply To: WP loop via checkbox value

  • 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);