How to set a WordPress custom post type visibility to private after editing?how to display custom data from custom post typesHow to stop Wordpress from saving custom post types to wp_postsWordpress: front-end edit a custom post type with only password loginCreate Custom Settings Page for WordPress Custom Post TypeWordpress - custom post types with custom fields, gone from admin menuCustomize WordPress default private visibility template pagehow to loop in single attachment posts in custom post type in wordpress?Wordpress Custom Post Type Variable Overlap?Wordpress's custom post type does not expose content with the_content filterSet permalink with parent and child custom categories in WordPress
If two black hole event horizons overlap (touch) can they ever separate again?
Procedurally generate regions on island
How to answer "write something on the board"?
Using Raspberry Pi to flash its own SD card
What does grep -v "grep" mean and do?
How is this practical and very old scene shot?
Should I share with a new service provider a bill from its competitor?
What's the easiest way for a whole party to be able to communicate with a creature that doesn't know Common?
What's the rule for a natural 20 on a Perception check?
Most elegant way to write a one shot IF
In native German words, is Q always followed by U, as in English?
How to Prove a System Is Invertible?
Is the location of an aircraft spoiler really that vital?
Who voices the character "Finger" in The Fifth Element?
How did Lefschetz do mathematics without hands?
Why is Silver Fang rated as S-class Rank 3 hero?
cannot execute script while its permission is 'x'
What was the impact of Fischer vs. Spassky 1972 on the relationship between the USA and the Soviet Union?
How did installing this RPM create a file?
Cast volatile array to non volatile array
Do the 26 richest billionaires own as much wealth as the poorest 3.8 billion people?
Donkey as Democratic Party symbolic animal
Why won't the ground take my seed?
How to add text on top of symbols for vector layers in QGIS
How to set a WordPress custom post type visibility to private after editing?
how to display custom data from custom post typesHow to stop Wordpress from saving custom post types to wp_postsWordpress: front-end edit a custom post type with only password loginCreate Custom Settings Page for WordPress Custom Post TypeWordpress - custom post types with custom fields, gone from admin menuCustomize WordPress default private visibility template pagehow to loop in single attachment posts in custom post type in wordpress?Wordpress Custom Post Type Variable Overlap?Wordpress's custom post type does not expose content with the_content filterSet permalink with parent and child custom categories in WordPress
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I created a custom post type with a plugin. A registered user can insert a new post from front-end and it is saved as draft. When I edit it in back-end I need it is saved with private visibility.
I found this snippet to set visibility by default:
public function force_dpa_request_private( $data , $postarr )
if( empty( $data['post_name'] ) && 'my-cpt' == $postarr['post_type'] )
$data[ 'post_status' ] = 'private';
return $data;
but it works only on first insert, when I edit it the visibility change to public...
wordpress visibility custom-post-type private
add a comment |
I created a custom post type with a plugin. A registered user can insert a new post from front-end and it is saved as draft. When I edit it in back-end I need it is saved with private visibility.
I found this snippet to set visibility by default:
public function force_dpa_request_private( $data , $postarr )
if( empty( $data['post_name'] ) && 'my-cpt' == $postarr['post_type'] )
$data[ 'post_status' ] = 'private';
return $data;
but it works only on first insert, when I edit it the visibility change to public...
wordpress visibility custom-post-type private
add a comment |
I created a custom post type with a plugin. A registered user can insert a new post from front-end and it is saved as draft. When I edit it in back-end I need it is saved with private visibility.
I found this snippet to set visibility by default:
public function force_dpa_request_private( $data , $postarr )
if( empty( $data['post_name'] ) && 'my-cpt' == $postarr['post_type'] )
$data[ 'post_status' ] = 'private';
return $data;
but it works only on first insert, when I edit it the visibility change to public...
wordpress visibility custom-post-type private
I created a custom post type with a plugin. A registered user can insert a new post from front-end and it is saved as draft. When I edit it in back-end I need it is saved with private visibility.
I found this snippet to set visibility by default:
public function force_dpa_request_private( $data , $postarr )
if( empty( $data['post_name'] ) && 'my-cpt' == $postarr['post_type'] )
$data[ 'post_status' ] = 'private';
return $data;
but it works only on first insert, when I edit it the visibility change to public...
wordpress visibility custom-post-type private
wordpress visibility custom-post-type private
asked Mar 25 at 13:27
icolumbroicolumbro
175 bronze badges
175 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can hook to the save_post
which is called after the post is created or updated.
<?php
add_action( 'save_post', 'callback_save_post', 10, 3);
function callback_save_post( $post_ID, $post, $update )
if ( 'my-cpt' === get_post_type( $post_ID) && ! wp_is_post_revision( $post_ID ) )
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'callback_save_post', 10 );
// Make the post private if it is edited else make it draft.
if ( $update )
$postarr = array(
'ID' => $post_ID,
'post_status' => 'private'
);
else
$postarr = array(
'ID' => $post_ID,
'post_status' => 'draft'
);
// Update the post.
wp_update_post( $postarr );
// re-hook this function.
add_action( 'save_post', 'callback_save_post', 10, 3);
Reference:
https://developer.wordpress.org/reference/hooks/save_post/
https://codex.wordpress.org/Function_Reference/wp_update_post
Unfortunately this doesn't work. I think there is a syntax error on line 5 (remove_action accepts just three parameters) but however the system seems to stuck (very slow to save post) and after submit a get an incomplete page on front-end. On back-end I find the post but it continues to have private visibility not draft...
– icolumbro
Mar 25 at 15:20
@icolumbro I fixed the code. Can you show us the error?
– Sagar Bahadur Tamang
Mar 25 at 15:32
It goes in infinite loop because if I var_dump($update) after saving a post a get a full never ending bool(true) page... :(
– icolumbro
Mar 25 at 15:39
@icolumbro Ok let me check it.
– Sagar Bahadur Tamang
Mar 25 at 15:40
some news? Thank you!
– icolumbro
Mar 26 at 8:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55338903%2fhow-to-set-a-wordpress-custom-post-type-visibility-to-private-after-editing%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can hook to the save_post
which is called after the post is created or updated.
<?php
add_action( 'save_post', 'callback_save_post', 10, 3);
function callback_save_post( $post_ID, $post, $update )
if ( 'my-cpt' === get_post_type( $post_ID) && ! wp_is_post_revision( $post_ID ) )
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'callback_save_post', 10 );
// Make the post private if it is edited else make it draft.
if ( $update )
$postarr = array(
'ID' => $post_ID,
'post_status' => 'private'
);
else
$postarr = array(
'ID' => $post_ID,
'post_status' => 'draft'
);
// Update the post.
wp_update_post( $postarr );
// re-hook this function.
add_action( 'save_post', 'callback_save_post', 10, 3);
Reference:
https://developer.wordpress.org/reference/hooks/save_post/
https://codex.wordpress.org/Function_Reference/wp_update_post
Unfortunately this doesn't work. I think there is a syntax error on line 5 (remove_action accepts just three parameters) but however the system seems to stuck (very slow to save post) and after submit a get an incomplete page on front-end. On back-end I find the post but it continues to have private visibility not draft...
– icolumbro
Mar 25 at 15:20
@icolumbro I fixed the code. Can you show us the error?
– Sagar Bahadur Tamang
Mar 25 at 15:32
It goes in infinite loop because if I var_dump($update) after saving a post a get a full never ending bool(true) page... :(
– icolumbro
Mar 25 at 15:39
@icolumbro Ok let me check it.
– Sagar Bahadur Tamang
Mar 25 at 15:40
some news? Thank you!
– icolumbro
Mar 26 at 8:50
add a comment |
You can hook to the save_post
which is called after the post is created or updated.
<?php
add_action( 'save_post', 'callback_save_post', 10, 3);
function callback_save_post( $post_ID, $post, $update )
if ( 'my-cpt' === get_post_type( $post_ID) && ! wp_is_post_revision( $post_ID ) )
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'callback_save_post', 10 );
// Make the post private if it is edited else make it draft.
if ( $update )
$postarr = array(
'ID' => $post_ID,
'post_status' => 'private'
);
else
$postarr = array(
'ID' => $post_ID,
'post_status' => 'draft'
);
// Update the post.
wp_update_post( $postarr );
// re-hook this function.
add_action( 'save_post', 'callback_save_post', 10, 3);
Reference:
https://developer.wordpress.org/reference/hooks/save_post/
https://codex.wordpress.org/Function_Reference/wp_update_post
Unfortunately this doesn't work. I think there is a syntax error on line 5 (remove_action accepts just three parameters) but however the system seems to stuck (very slow to save post) and after submit a get an incomplete page on front-end. On back-end I find the post but it continues to have private visibility not draft...
– icolumbro
Mar 25 at 15:20
@icolumbro I fixed the code. Can you show us the error?
– Sagar Bahadur Tamang
Mar 25 at 15:32
It goes in infinite loop because if I var_dump($update) after saving a post a get a full never ending bool(true) page... :(
– icolumbro
Mar 25 at 15:39
@icolumbro Ok let me check it.
– Sagar Bahadur Tamang
Mar 25 at 15:40
some news? Thank you!
– icolumbro
Mar 26 at 8:50
add a comment |
You can hook to the save_post
which is called after the post is created or updated.
<?php
add_action( 'save_post', 'callback_save_post', 10, 3);
function callback_save_post( $post_ID, $post, $update )
if ( 'my-cpt' === get_post_type( $post_ID) && ! wp_is_post_revision( $post_ID ) )
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'callback_save_post', 10 );
// Make the post private if it is edited else make it draft.
if ( $update )
$postarr = array(
'ID' => $post_ID,
'post_status' => 'private'
);
else
$postarr = array(
'ID' => $post_ID,
'post_status' => 'draft'
);
// Update the post.
wp_update_post( $postarr );
// re-hook this function.
add_action( 'save_post', 'callback_save_post', 10, 3);
Reference:
https://developer.wordpress.org/reference/hooks/save_post/
https://codex.wordpress.org/Function_Reference/wp_update_post
You can hook to the save_post
which is called after the post is created or updated.
<?php
add_action( 'save_post', 'callback_save_post', 10, 3);
function callback_save_post( $post_ID, $post, $update )
if ( 'my-cpt' === get_post_type( $post_ID) && ! wp_is_post_revision( $post_ID ) )
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'callback_save_post', 10 );
// Make the post private if it is edited else make it draft.
if ( $update )
$postarr = array(
'ID' => $post_ID,
'post_status' => 'private'
);
else
$postarr = array(
'ID' => $post_ID,
'post_status' => 'draft'
);
// Update the post.
wp_update_post( $postarr );
// re-hook this function.
add_action( 'save_post', 'callback_save_post', 10, 3);
Reference:
https://developer.wordpress.org/reference/hooks/save_post/
https://codex.wordpress.org/Function_Reference/wp_update_post
edited Mar 25 at 15:31
answered Mar 25 at 14:38
Sagar Bahadur TamangSagar Bahadur Tamang
1,3911 gold badge11 silver badges25 bronze badges
1,3911 gold badge11 silver badges25 bronze badges
Unfortunately this doesn't work. I think there is a syntax error on line 5 (remove_action accepts just three parameters) but however the system seems to stuck (very slow to save post) and after submit a get an incomplete page on front-end. On back-end I find the post but it continues to have private visibility not draft...
– icolumbro
Mar 25 at 15:20
@icolumbro I fixed the code. Can you show us the error?
– Sagar Bahadur Tamang
Mar 25 at 15:32
It goes in infinite loop because if I var_dump($update) after saving a post a get a full never ending bool(true) page... :(
– icolumbro
Mar 25 at 15:39
@icolumbro Ok let me check it.
– Sagar Bahadur Tamang
Mar 25 at 15:40
some news? Thank you!
– icolumbro
Mar 26 at 8:50
add a comment |
Unfortunately this doesn't work. I think there is a syntax error on line 5 (remove_action accepts just three parameters) but however the system seems to stuck (very slow to save post) and after submit a get an incomplete page on front-end. On back-end I find the post but it continues to have private visibility not draft...
– icolumbro
Mar 25 at 15:20
@icolumbro I fixed the code. Can you show us the error?
– Sagar Bahadur Tamang
Mar 25 at 15:32
It goes in infinite loop because if I var_dump($update) after saving a post a get a full never ending bool(true) page... :(
– icolumbro
Mar 25 at 15:39
@icolumbro Ok let me check it.
– Sagar Bahadur Tamang
Mar 25 at 15:40
some news? Thank you!
– icolumbro
Mar 26 at 8:50
Unfortunately this doesn't work. I think there is a syntax error on line 5 (remove_action accepts just three parameters) but however the system seems to stuck (very slow to save post) and after submit a get an incomplete page on front-end. On back-end I find the post but it continues to have private visibility not draft...
– icolumbro
Mar 25 at 15:20
Unfortunately this doesn't work. I think there is a syntax error on line 5 (remove_action accepts just three parameters) but however the system seems to stuck (very slow to save post) and after submit a get an incomplete page on front-end. On back-end I find the post but it continues to have private visibility not draft...
– icolumbro
Mar 25 at 15:20
@icolumbro I fixed the code. Can you show us the error?
– Sagar Bahadur Tamang
Mar 25 at 15:32
@icolumbro I fixed the code. Can you show us the error?
– Sagar Bahadur Tamang
Mar 25 at 15:32
It goes in infinite loop because if I var_dump($update) after saving a post a get a full never ending bool(true) page... :(
– icolumbro
Mar 25 at 15:39
It goes in infinite loop because if I var_dump($update) after saving a post a get a full never ending bool(true) page... :(
– icolumbro
Mar 25 at 15:39
@icolumbro Ok let me check it.
– Sagar Bahadur Tamang
Mar 25 at 15:40
@icolumbro Ok let me check it.
– Sagar Bahadur Tamang
Mar 25 at 15:40
some news? Thank you!
– icolumbro
Mar 26 at 8:50
some news? Thank you!
– icolumbro
Mar 26 at 8:50
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55338903%2fhow-to-set-a-wordpress-custom-post-type-visibility-to-private-after-editing%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown