I have an old WordPress installation. Unfortunately, public posts and drafts are displayed on the front. I think it's because of the wordpress loop. The following code is used:
<section class="container container-fluid">
<div id="layout" style="padding: 0 -15px;">
<?php
if (has_tag('Karriere'))
ini_set("display_errors", 1);
$args = array('offset' => 0,
'numberposts' => 12,
'limit' => 12,
'post_type' => "post",
'orderby' => 'post_date',
'order' => 'DESC',
'category' => '26,60',
'tag' => 'Karriere');
//$args = array('numberposts' => 2);
$recent_posts = wp_get_recent_posts($args, ARRAY_A);
$_SESSION['home_offset'] = count($recent_posts);
foreach( $recent_posts as $recent ){
$post_uri = get_permalink($recent['ID']);
//$post_image = wp_get_attachment_url( get_post_thumbnail_id($recent['ID']) );
$post_image = wp_get_attachment_image_src( get_post_thumbnail_id($recent['ID']), 'vorschaubild');
$post_image = $post_image[0];
$post_categoryList = array();
$post_categories = get_the_category($recent['ID']);
foreach($post_categories as $post_category){
$post_categoryList[] = $post_category->slug;
}
$content = explode("<!--more-->", $recent['post_content']);
$content = $content[0];
$content = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $content);
$content = preg_replace('/<div id=\"(.*?)\" class=\"(.*?)\">>(.*?)<\/div>/', "", $content);
?>
<div>
<div class="wrapper-karriere-box" style="padding: 0 5px;">
<? //item echo htmlspecialchars($content); ?>
<a href="<?=$post_uri?>" title="<?=$recent["post_title"]?>">
<div class="newsBox" data-categories="<?=implode(",", $post_categoryList)?>">
<?php
if($post_image !== false && $post_image !== NULL){
?>
<img src="<?=$post_image?>" style="width: 100%;" />
<?php } ?>
<div class="news-box-headline">
<h3><?=$recent["post_title"]?></h3>
<p><?=$content?></p>
</div>
</div>
</a>
</div>
</div>
<?php } ?>
</div>
what i have to change that public posts and drafts are not shown in frontend (for unregistered users).
I hope somebody can help! Thanks a lot!
I have an old WordPress installation. Unfortunately, public posts and drafts are displayed on the front. I think it's because of the wordpress loop. The following code is used:
<section class="container container-fluid">
<div id="layout" style="padding: 0 -15px;">
<?php
if (has_tag('Karriere'))
ini_set("display_errors", 1);
$args = array('offset' => 0,
'numberposts' => 12,
'limit' => 12,
'post_type' => "post",
'orderby' => 'post_date',
'order' => 'DESC',
'category' => '26,60',
'tag' => 'Karriere');
//$args = array('numberposts' => 2);
$recent_posts = wp_get_recent_posts($args, ARRAY_A);
$_SESSION['home_offset'] = count($recent_posts);
foreach( $recent_posts as $recent ){
$post_uri = get_permalink($recent['ID']);
//$post_image = wp_get_attachment_url( get_post_thumbnail_id($recent['ID']) );
$post_image = wp_get_attachment_image_src( get_post_thumbnail_id($recent['ID']), 'vorschaubild');
$post_image = $post_image[0];
$post_categoryList = array();
$post_categories = get_the_category($recent['ID']);
foreach($post_categories as $post_category){
$post_categoryList[] = $post_category->slug;
}
$content = explode("<!--more-->", $recent['post_content']);
$content = $content[0];
$content = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $content);
$content = preg_replace('/<div id=\"(.*?)\" class=\"(.*?)\">>(.*?)<\/div>/', "", $content);
?>
<div>
<div class="wrapper-karriere-box" style="padding: 0 5px;">
<? //item echo htmlspecialchars($content); ?>
<a href="<?=$post_uri?>" title="<?=$recent["post_title"]?>">
<div class="newsBox" data-categories="<?=implode(",", $post_categoryList)?>">
<?php
if($post_image !== false && $post_image !== NULL){
?>
<img src="<?=$post_image?>" style="width: 100%;" />
<?php } ?>
<div class="news-box-headline">
<h3><?=$recent["post_title"]?></h3>
<p><?=$content?></p>
</div>
</div>
</a>
</div>
</div>
<?php } ?>
</div>
what i have to change that public posts and drafts are not shown in frontend (for unregistered users).
I hope somebody can help! Thanks a lot!
Share Improve this question asked May 15, 2020 at 9:12 tom84tom84 2551 gold badge6 silver badges21 bronze badges1 Answer
Reset to default 1To see only the "publish" post, you have to specified the "post_status".
The documentation of the wp_get_recent_posts function said, that if no "post_status" is specified the function used the default values "draft, publish, future, pending, private".
$defaults = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true,
);
To distinguish between logged in and unlogged users, you could use the function is_user_logged_in