As @s_ha_dum already pointed out, you can use the built-in private posts status function when publishing posts. This will hide these posts from all logged out users.
As you have already gone down the custom fields route, you can make the following changes to your custom post type archive
-
Remove your custom query. This messes up page functionalities and pagination. I have written an answer on this a while ago on where to use custom queries and where not
-
Use
pre_get_posts
as described in my linked post to alter the main query accordingly. This will solve all your issues.
NOTE: By default, the post_type
used by WP_Query
is post
. You probably have no default posts which fits your query, that is why you don’t get any posts
You need to do something like this in functions.php after reverting to the default loop in your post type archive page:
NOTE: Only sample code, adjust as needed. Requires PHP 5.3+)
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() // VERY important, targets only front end queries
&& $q->is_main_query() // VERY important, targets only main query
&& $q->is_post_type_archive( 'YOUR_POST_TYPE_NAME' )
// Which post type archive page to target.
// for example: && $q->is_post_type_archive( 'works' )
) {
$q->set( 'meta_key', 'META_KEY_NAME' );
//for example: $q->set( 'post_status', 'publish' );
$q->set( 'meta_value', 'META_VALUE_VALUE');
// Rest of your arguments to set
}
});