1

I am trying to save post meta when the post is viewed containing key userid:x with value of the current post's ID. This information lets me know if the post has been read by current user (making notification in sidebar out of it afterwards).

I do this by running this code in the loop of single.php right after the_content();:

<?php if ( in_category( 'oznamy' )) { 

    $current_user = wp_get_current_user();
    $currentuserid = $current_user->ID;
    $towrite = "userid:";
    $meta_key = $towrite . $currentuserid;

    $meta_value = $post->ID ;
    add_post_meta($post->ID , $meta_key, $meta_value, true); 

} ?>

It does save the meta value of the currently displayed post, however it always saves the meta for another post as well.

The meta value is set to the post ID just to check what it saves to different post. It saves the different post's ID but that post is NOT displayed in the single.php loop...

So, if I view post 676 on the front end, it saves meta key userid:1 and value 676, which is what I want. However, it also saves meta for key userid:1 and value 674 for post ID 674 which was NOT displayed.

Why would single.php even touch the different ID?

UPDATE : Whole Single.php code is following:

<?php get_header(); ?>

<?php get_template_part('template-part', 'head'); ?>

<?php get_template_part('template-part', 'topnav'); ?>

<!-- start content container -->
<div class="row dmbs-content">

    <?php //left sidebar ?>
    <?php get_sidebar( 'left' ); ?>

    <div class="col-md-<?php devdmbootstrap3_main_content_width(); ?> dmbs-main">

        <?php

            //if this was a search we display a page header with the results count. If there were no results we display the search form.
            if (is_search()) :

                 $total_results = $wp_query->found_posts;

                 echo "<h2 class='page-header'>" . sprintf( __('%s Search Results for "%s"','devdmbootstrap3'),  $total_results, get_search_query() ) . "</h2>";

                 if ($total_results == 0) :
                     get_search_form(true);
                 endif;

            endif;

        ?>


<?php
    global $current_user;
    get_currentuserinfo();

?>


<?php if( is_user_member_of_blog( $current_user->ID ) ): ?>

<?php if( is_user_logged_in() ): ?>
<script>
$("#menu-item-508").addClass("active");
</script>



            <?php // theloop
                if ( have_posts() ) : while ( have_posts() ) : the_post();

                    // single post
                    if ( is_single() ) : ?>

                        <div <?php post_class(); ?>>

                            <h2 class="page-header-single"><?php the_title() ;?></h2>
                            <?php if ( has_post_thumbnail() ) : ?>
                                <?php the_post_thumbnail(); ?>
                                <div class="clear"></div>
                            <?php endif; ?>

                            <?php the_content(); ?>


<!-- zapis id usera ktory precital oznam -->
<?php if ( in_category( 'oznamy' )) { 


  $current_user = wp_get_current_user();
  $currentuserid = $current_user->ID;
  $towrite = "userid:";
  $meta_key = $towrite . $currentuserid;




$meta_value = $post->ID ;
add_post_meta($meta_value , $meta_key, $meta_value, true); 


} ?>

<!-- koniec zapis id usera ktory precital oznam -->



                            <?php wp_link_pages(); ?>
                            <?php get_template_part('template-part', 'postmeta'); ?>

Komentáre:
                            <?php comments_template(); ?>

                        </div>
                    <?php
                    // list of posts
                    else : ?>
                       <div <?php post_class(); ?>>

                            <h2 class="page-header">
                                <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'devdmbootstrap3' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                            </h2>

                            <?php if ( has_post_thumbnail() ) : ?>
                               <?php the_post_thumbnail(); ?>
                                <div class="clear"></div>
                            <?php endif; ?>
                            <?php the_content(); ?>
                            <?php wp_link_pages(); ?>
                            <?php get_template_part('template-part', 'postmeta'); ?>
                            <?php  if ( comments_open() ) : ?>
                                   <div class="clear"></div>
                                  <p class="text-right">
                                      <a class="btn btn-success" href="<?php the_permalink(); ?>#comments"><?php comments_number(__('Leave a Comment','devdmbootstrap3'), __('One Comment','devdmbootstrap3'), '%' . __(' Comments','devdmbootstrap3') );?> <span class="glyphicon glyphicon-comment"></span></a>
                                  </p>
                            <?php endif; ?>
                       </div>

                     <?php  endif; ?>

                <?php endwhile; ?>
                <?php posts_nav_link(); ?>
                <?php else: ?>

                    <?php get_404_template(); ?>

            <?php endif; ?>
<?php else:
  wp_die('Unauthorized.');
endif; ?>

<?php else:
  wp_die('Unauthorized.');
endif; ?>
   </div>

   <?php //get the right sidebar ?>
   <?php get_sidebar( 'right' ); ?>

</div>
<!-- end content container -->

<?php get_footer(); ?>
  • The simplest explanation is that you're making multiple requests. Have you ruled out things like browser pre-fetch? Are you logging requests to confirm that these are actually happening on the same request? – Milo Jul 18 '17 at 13:06
0

The proper way would be to check if you are on a single page, and if so, then check if your post has the oznamy set as one of its categories.

Let's rewrite your code the following way:

if ( is_single() && in_category( 'oznamy' , get_the_ID() ) ) {
    // Get the current user
    $current_user = wp_get_current_user();
    $currentuserid = $current_user->ID;
    // Update the post's meta
    update_post_meta($post->ID , 'userid:'.$currentuserid , $post->ID); 
} 

Important notes

  • This code should be used in functions.php instead of directly being used in the template files
  • You might be visiting another post by accident, that causes to save metadata for that post too. You didn't post your single.php's content, so I can't tell if you have another post being loaded aside.
  • You should use update_post_meta instead of add_post_meta unless you want to end up with a million of metadata saved on each visit.
  • There is a second argument possible to be set in the in_category() function, which is the post's ID. This might solve your problem, by only setting the current post's ID. is_single() also accepts the same argument.
  • Thank you @Jack Johansson for the answer. I do check if is_single. I use add_post_meta with parameter true (unique) in the end which does NOT add new meta if it allready exists (so I dont need to check that..) . I am updating my first post with whole single.php code. (I DID try to remove everything unnacessary from it just to display single post - this is basicly index.php from my theme (which is also responsible for displaying single post) modified to single.php) - this did not help me either :/ – Erik G Jul 18 '17 at 8:09
  • I did try to use your code instead of mine. It does exactly the same thing, also adding meta to second different post which was not displayed. – Erik G Jul 18 '17 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.