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;








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...










share|improve this question




























    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...










    share|improve this question
























      0












      0








      0


      1






      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...










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 13:27









      icolumbroicolumbro

      175 bronze badges




      175 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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






          share|improve this answer

























          • 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










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



          );













          draft saved

          draft discarded


















          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









          0














          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






          share|improve this answer

























          • 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















          0














          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






          share|improve this answer

























          • 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













          0












          0








          0







          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






          share|improve this answer















          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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

















          • 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








          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.



















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript