• Resolved kizha

    (@kizha)


    Hello,
    I have created a book review web application using WordPress, and I have created a custom post type ‘book’ along with custom fields such as ‘author’ using ACF plugin.
    In my single-book.php template, I want to show books that belong to the same author at the bottom of the page.
    My problem is, I don’t know how to assign the author field to the meta value. This is the code I’m using which works perfectly when I specify the author for example ‘Paula Hawkins’:
    <?php
    $posts = get_posts(array(
    ‘numberposts’ => -1,
    ‘post_type’ => ‘book’,
    ‘meta_key’ => ‘author’,
    ‘meta_value’ => ‘Paula Hawkins’,
    ‘post__not_in’ => array( $post->ID )
    ));

    if( $posts ): ?>

    <?php wp_reset_postdata(); ?>
    <?php endif;
    ?>
    instead of specifying the author, I want to get the author using code that will work for all pages using this template of course.
    i tried saving the author in a varibale like this:
    $author = the_field(‘author’);
    and then changed the meta value field to this:
    ‘meta_value’ => $author,

    but its not working.

    • This topic was modified 4 years ago by kizha.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Ian Sackofwits

    (@plantprogrammer)

    Hey, kizha, it sounds like $author may be null when you’re attempting to access it.

    Where did you declare $author?

    Thread Starter kizha

    (@kizha)

    Hello Ian, thanks for the reply, but the variable isn’t null because when I echo it, it shows the author of the book correctly. My bad for not sharing this part of the code in the first place:
    <?php
    $author = the_field(‘author’);
    echo $author;
    //code that shows books by same author
    $posts = get_posts(array(
    ‘numberposts’ => -1,
    ‘post_type’ => ‘book’,
    ‘meta_key’ => ‘author’,
    ‘meta_value’ => $author,
    ‘post__not_in’ => array( $post->ID )

    ));

    Moderator bcworkz

    (@bcworkz)

    You have the right idea. Other code can influence attempts to get_posts(), which could cause trouble when you do so. Use the query monitor plugin, which allows you to examine the SQL of all queries. Locate the query made by your call to get_posts() and examine it for unexpected criteria which would prevent the query from working as you expect.

    Thread Starter kizha

    (@kizha)

    Thanks for the reply, but as I’m not so good with the technical stuff, I’m not sure I understand. I haven’t used PHP before but I believe you’re saying that the query itself is wrong?
    if so, like I said when I specify the author myself by typing in ‘meta_value’ => ‘Paula Hawkins’, for example. But when I put in $author or even leave it empty, it shows books by all authors I have instead of the one specific to the current book’s page. So I’m wondering how I can assign the function “the_field(‘author’);” to the meta value.

    Thread Starter kizha

    (@kizha)

    For anyone having the same issue, I solved the problem by using the code below:
    $author = get_post_meta( get_the_ID(), 'author', true);
    instead of
    $author = the_field(‘author’);

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Giving variable to meta_value’ is closed to new replies.