0

I am new to developing themes with wordpress, so here's my question. Can I create my own query in wordpress with traditional methods?

I know we use the WP query methods, but what is the most efficient way, and can I use my own SQL commands to generate a query for all tags with published post? for example.

I am using get_the_tags() with wordpress, but it is only providing the tags related to the post. I want all tags with published post to display. And come to think about it now, I think it isn't even displaying the tags, just the post category, so I must be doing something wrong.

Below is the code in the functions.php file

 function get_all_tags(){
    $posttags = get_the_tags('');
    if ($posttags) {
        foreach($posttags as $tag) {
            echo '<li><a href="#">'.$tag->name.'</a></li>'; 
        }
    }
}

In the single.php file, i am calling on this function.

<?php $tagsAll = get_all_tags(); echo $tagsAll; ?>

I picked up some of this from online searches, but I am trying to modify for all tags with little success. Thanks

4
  • 1
    Use get_tags(). You should almost never need to use manual SQL queries. Commented Nov 13, 2022 at 1:26
  • I agree with Jacob. You could use SQL, but there is no need.
    – rudtek
    Commented Nov 13, 2022 at 4:26
  • Thanks for the follow up fellas, I did make a small change based on your recommendations @JacobPeattie and now I can see all the tags in the list. I wish I could give you both a vote but, I guess I don't have enough reputation score to do so. One quick question for both of you , in the get_tags() link provided, I see this code: get_tags( string|array $args = '' ): WP_Term[]|int|WP_Error Is it necessary to use a variable like $args as an option, or can I just use the empty apostrophes on their own, without the variable to store it in? Does that change the purpose in any way? Commented Nov 13, 2022 at 12:10
  • I have this other question that hasn't been answered, maybe you guys can point me in the right direction. Thanks again for your assistance. stackoverflow.com/questions/74416964/… Commented Nov 13, 2022 at 12:24

1 Answer 1

0

With some assistance from @JacobPeattie I modified the code to the recommended get_tags() function. Now in the functions.php I have the following code.

// Get al the tags with published post
function get_all_tags(){
    $posttags = get_tags('');
    if ($posttags) {
        foreach($posttags as $tag) {
            echo '<li><a href="#">'.$tag->name.'</a></li>'; 
        }
    }
}

In the single.php file, I call this with the following code.

<?php $tagsAll = get_all_tags(); echo $tagsAll; ?>

Your Answer

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

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