2

I'm creating a PHP class for a Wordpress site. The following method works fine, but when I try to abstract it a bit by passing an argument and calling it within another method (see code further down below), it doesn't work anymore.

public function user_has_submitted_in_dog_category() {

    $args = array(
        'post_type' => 'submissions',
        'author' => get_current_user_id(),
        'tax_query' => array(
            array(
                'taxonomy' => 'submissions_categories',
                'field' => 'slug',
                'terms' => 'dog'
            )
        )
    );

    $user_posts = get_posts( $args );

    if( count( $user_posts ) )
        return true;

}

This is not working:

public function user_has_submitted_in_dog_category() {

    $this->user_has_submitted_in_animal_category( 'dog' );

}

public function user_has_submitted_in_animal_category( $category ) {

    $args = array(
        'post_type' => 'submissions',
        'author' => get_current_user_id(),
        'tax_query' => array(
            array(
                'taxonomy' => 'submissions_categories',
                'field' => 'slug',
                'terms' => $category
            )
        )
    );

    $user_posts = get_posts( $args );

    if( count( $user_posts ) )
        return true;

}

By not working, I mean that user_has_submitted_in_dog_category() is not returning true. I call it in a template file as follows:

<?php if( $submission->user_has_submitted_in_dog_category() ) : ?>
    <div class="msg">You have already submitted.</div>
<?php else : ?>
    <div class="msg">You have not submitted yet.</div>
<?php endif; ?>

The block of code prints You have not submitted yet, but I do have posts in my custom taxonomy dog.

4
  • How are you calling user_has_submitted_in_dog_category() Commented Dec 15, 2017 at 18:45
  • 3
    How do you define "not working"? Are you getting an error or something? What is the expected behavior versus what you're actually seeing? (hint: you probably want to put return in front of $this->user_has_submitted_in_animal_category( 'dog' ); otherwise the return value won't get passed along when you call the outer function.) Commented Dec 15, 2017 at 18:47
  • Can you be more specific about "doesn't work" means? Error thrown? Nothing happens? Keep in mind that PHP's error output is turned off by default, so if you're getting no output, you may find it helpful to turn it on temporarily, or use a test environment where it's turned on. Commented Dec 15, 2017 at 18:47
  • Without having the complete class definition and instantiation code, it is hard to properly assess the problem. Commented Dec 15, 2017 at 20:55

2 Answers 2

1

You need to return from your first (dog) method.

public function user_has_submitted_in_dog_category() {

    return $this->user_has_submitted_in_animal_category( 'dog' );

}
Sign up to request clarification or add additional context in comments.

Comments

0

Wordpress might be calling the method statically. You might be best off doing that yourself as the method doesn't seem to need an object instance to work:

public function user_has_submitted_in_dog_category() {

  MyClass::user_has_submitted_in_animal_category( 'dog' );

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.