I have a custom post type 'Works', and an archive page 'archive-works.php'. I would like to hide the non-public posts from the archive page, so I added a custom meta box.
I also modified the query to list the posts only, when the public_s is not 0:
$args = array(
'meta_key' => 'public_s',
'meta_value' => '0',
'meta_compare' => '!='
);
$queryWorks = new WP_Query( $args );
if ( $queryWorks->have_posts() ) : while ( $queryWorks->have_posts() ) : $queryWorks->the_post();
For some reasons the query doesn't work.
var_dump($queryWorks->the_post()); -> NULL
What's the problem with the query?
I have a custom post type 'Works', and an archive page 'archive-works.php'. I would like to hide the non-public posts from the archive page, so I added a custom meta box.
I also modified the query to list the posts only, when the public_s is not 0:
$args = array(
'meta_key' => 'public_s',
'meta_value' => '0',
'meta_compare' => '!='
);
$queryWorks = new WP_Query( $args );
if ( $queryWorks->have_posts() ) : while ( $queryWorks->have_posts() ) : $queryWorks->the_post();
For some reasons the query doesn't work.
var_dump($queryWorks->the_post()); -> NULL
What's the problem with the query?
Share Improve this question edited Nov 18, 2019 at 0:00 Viktor Borítás 3042 silver badges11 bronze badges asked Jun 3, 2015 at 19:02 user1452062user1452062 5271 gold badge10 silver badges21 bronze badges 2- Why aren't you using the built in "private" functionality? – s_ha_dum Commented Jun 3, 2015 at 19:15
- Good idea, I forgot to use that. I used this method on the whole site, and in the functions and it's too much time to replace everywhere. I think it's easier to fix the query. I hope you have an idea. :) – user1452062 Commented Jun 3, 2015 at 19:26
1 Answer
Reset to default 2As @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
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744993995a4605076.html
评论列表(0条)