[wp-trac] [WordPress Trac] #40253: the_content problem

WordPress Trac noreply at wordpress.org
Fri Mar 24 21:43:28 UTC 2017


#40253: the_content problem
-------------------------------+------------------------------
 Reporter:  tazotodua          |       Owner:
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Posts, Post Types  |     Version:
 Severity:  normal             |  Resolution:
 Keywords:                     |     Focuses:
-------------------------------+------------------------------

Old description:

> Hi.
> I think it is quite essential problem.
>
> lets say I want to check, if post's custom field "smth" is 1, then I want
> to set content to "Hello World".
>
> add_filter('the_content', function($content){
>     global $post;
>     if(get_post_meta($post->ID, 'smth', true ) == 1 ){
>       $content = "Hello World";
>     }
>     return $content;
> });
>

> but problem raises, when we access single post, which has set it's custom
> field to `smth` to 1, but also,  `the_content` is also changed to `Hello
> World` everywhere else (in sidebars, recent posts and etc... everywhere),
> because in `the_content` we make comparison for the GLOBAL POST's data.
> I think there should be some kind of additional parameter with
> `the_content` filter, which will indicate if it is the main content of
> single page.
>

>

> the same problem (but oppositely) arises, when we want to filter
> `the_content` in all areas, but not for the main post-s content..

New description:

 Hi.
 I think it is quite essential problem.

 lets say I want to check, if post's custom field "smth" is 1, then I want
 to set content to "Hello World".
 {{{
 add_filter('the_content', function($content){
     global $post;
     if(get_post_meta($post->ID, 'smth', true ) == 1 ){
       $content = "Hello World";
     }
     return $content;
 });
 }}}

 but problem raises, when we access single post, which has set it's custom
 field to `smth` to 1, but also,  `the_content` is also changed to `Hello
 World` everywhere else (in sidebars, recent posts and etc... everywhere),
 because in `the_content` we make comparison for the GLOBAL POST's data.  I
 think there should be some kind of additional parameter with `the_content`
 filter, which will indicate if it is the main content of single page.




 the same problem (but oppositely) arises, when we want to filter
 `the_content` in all areas, but not for the main post-s content..

--

Comment (by SergeyBiryukov):

 Replying to [ticket:40253 tazotodua]:
 > but problem raises, when we access single post, which has set it's
 custom field to `smth` to 1, but also,  `the_content` is also changed to
 `Hello World` everywhere else (in sidebars, recent posts and etc...
 everywhere)

 You could use
 [https://codex.wordpress.org/Function_Reference/is_main_query
 is_main_query()] to prevent that:
 {{{
 add_filter('the_content', function($content){
     global $post;
     if ( is_main_query() && get_post_meta( $post->ID, 'smth', true ) == 1
 ) {
       $content = "Hello World";
     }
     return $content;
 });
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40253#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list