I have used a form with an Ajax action to pull post information through on submit.

It works a charm. However, I have used categories to separate the work into categories. One of which is the brand.

Within the page template the script I use works, but when used in the functions.php file. It doesn't pull the required results through.

I think it may be something to do with when the query to the post is triggered or how the add_action is set up.

Can someone please help me pull through the name of the category found within the ID of 31? When used in the functions.php.

Here is what I have written:

if( $query->have_posts() ) :
    while( $query->have_posts() ): $query->the_post();

        echo "<div class=\"portfolio-piece\" style=\"background-image: url(" . get_the_post_thumbnail_url() . ");\">";

            echo "<a href=\"" . get_the_permalink() . "\" class=\"box-link\"></a>";

            echo "<div class=\"portfolio-piece-hover\">";

            echo "</div>";

            echo "<div class=\"portfolio-piece-inner\">";

                echo "<h5 class=\"portfolio-tags\">" . the_tags('', ' / ', '') . "</h5>";
                echo "<h4 class=\"portfolio-title\">";
                echo the_title();
                echo "</h4>";

                echo "<h4 class=\"portfolio-company\">";

                    $brands = wp_get_post_terms( $post->ID, 'category', array(
                        'orderby' => 'name',
                        'order' => 'DESC',
                        'parent' => 31
                    ) );
                    foreach ( $brands as $brand ) {
                        echo $brand->name;

                echo "</h4>";

            echo "</div>";

        echo "</div>";


Where literally everything works but the portfolio-company / $brands = wp_get_post_terms part.

Thanks, Jason.


(Revised answer)

So I'm assuming that there are actually posts in the category (ID: 31), and based on the wp_get_post_terms() reference:

(int) (Optional) The Post ID. Does not default to the ID of the global $post. Default 0.

It's likely that you're not passing the proper post ID to the wp_get_post_terms() function:

wp_get_post_terms( $post->ID, 'category', array(...) );

In that code, you have $post and yet, it's not defined anywhere in your code as in the question.

So if you're trying to get the ID of the current post in the WordPress Loop, where the post data is stored in the global $post variable, then you should first make the variable accessible in your function/code using global $post; like so:

global $post;
// here and after this line, you may now use $post->ID
if( $query->have_posts() ) :

Or you could also use get_the_ID() without having to do global $post;, particularly if all you wanted is to get the post ID:

wp_get_post_terms( get_the_ID(), 'category', array(...) ); // here I don't use $post->ID

And it's probably worth mentioning that in standard templates such as page.php, the global $post variable is already "global-ed", hence you may access the $post anywhere in the template. (Well, not exactly "anywhere"; however, I'm not going to dig deeper into that in this answer..)

Additional Notes

This is probably just a typo, but in the code in the question, you got a syntax error here:

foreach ( $brands as $brand ) {
  echo $brand->name;
}; // <- remove that ;

And you forgot the closing endif;..

  • Thats some top tips pal, thank you. I have swapped the '$post->ID' for 'get_the_ID()'. Now I have working code! Amazing c: Mar 19 '19 at 14:07
  • I'm glad to hear that! But would you please "accept" the answer, so that people know the question has an accepted solution? :)
    – Sally CJ
    Mar 19 '19 at 15:29
  • And I hope you don't mind I edited the answer because it didn't seem properly worded... ;)
    – Sally CJ
    Mar 19 '19 at 15:29
  • Not at all! I'm glad we found a quick fix, I knew it would be something silly! Mar 19 '19 at 16:53
  • Sally - why has this persons question been closed? I have written two answers and I won't get any credit. Killing me inside... stackoverflow.com/questions/55221950/… Mar 19 '19 at 16:54

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.