• Hi!

    First of all, I’d like to say that I am not a PHP expert. I have a very basic knowledge.
    I usually find some snippets on the internet and then customize them a little bit.

    Now, I would like to know why this piece of code doesn’t work and the result of the function which I am trying to save to the meta field is always NULL.

    function show_text(){
    echo 'hello';
    }
    
    $show = show_text();
    update_post_meta(5625, 'meta_field3', $show);

    I have a much-complicated function whose results I would like to store as meta value but this is just an example and I think the principle is the same.
    Thank you.

    Radan

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter fkoomek

    (@fkoomek)

    Hello.

    Thank you, it works.

    But how would I save as a value result of this whole function?

      function show_me_synonyms_and_similar_words()
        {
    
                //query
                global $post;
                $postid = get_the_ID();
                $posttags = get_the_tags();
                //get synonyms
                if ($posttags)
                {
                    foreach ($posttags as $tag)
                    {
                        $slug = $tag->slug . " ";
                    }
                    $output .= $slug;
                }
    
                $args = ["post__not_in" => [$postid], "post_status" => "publish", "orderby" => "title", "order" => "ASC", "tag_slug__and" => $output, ];
    
                $myposts = get_posts($args);
                if ($posttags)
                {
                    echo "<h2 class='sym cust_text'>Synonyma:</h2>";
                    echo '<ul class="syn tab">';
    
                    foreach ($myposts as $post): ?>
    		<li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li> 
    	
    	<?php
                    endforeach;
                    wp_reset_postdata();
                    echo "</ul>";
                }
            
    
            //search similar words by keyword    
            $field_c = str_replace(' ', '', strtolower(get_field("search_core")));
            $field_exclude = str_replace(' ', '', strtolower(get_field("search_core2")));
            if ($field_c)
            {
                $actual_title = str_replace(' ', '', strtolower(get_the_title()));
                $s = get_search_query();
                $args = ["s" => $field_c, ];
                // The Query          
                $the_query = new WP_Query($args);
                if ($the_query->have_posts())
                {
                    $post_count = $the_query->found_posts;
                    if (str_contains(str_replace(' ', '', strtolower($actual_title)) , $field_c) && $post_count == 1)
                    {
                    }
                    else
                    {
                        _e("<h2 class='sim'>Podobná slova:</h2>");
                        echo "<ul>";
                        while ($the_query->have_posts())
                        {
                            $the_query->the_post();
                            if (str_replace(' ', '', strtolower(get_the_title())) !== $actual_title)
                            {
    
                                if (strpos(str_replace(' ', '', strtolower(get_the_title())) , $field_exclude) == false)
                                {
    ?>      
                     
                        <li>
                            <a class="sim_terms" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                        </li>
    						<?php
                                }
                            }
                        }
                        echo "</ul>";
                    }
                }
            }
        }
    Thread Starter fkoomek

    (@fkoomek)

    Ok. Thank you. Based on your answer, I was able to rewrite the code of my function and now it works:)

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Update post meta (php basic question)’ is closed to new replies.