Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4543516
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:32:32+00:00 2026-05-21T15:32:32+00:00

the following query is used in my WordPress blog, it gets the categories that

  • 0

the following query is used in my WordPress blog, it gets the categories that a user has posted in. When he has a post in the category, the name of the category is shown.

It is a very very slow query because it is a big database and I have problems with the hosting company.

I have 3 categories, with id 3 called News, 4 called Articles and 5 called Others. My code is:

<?php
$author = get_query_var('author');
$categories = $wpdb->get_results("
    SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description
    FROM $wpdb->posts as posts
    LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
    LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
    LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
    WHERE 1=1 AND (
        posts.post_status = 'publish' AND
        posts.post_author = '{$post->post_author}' AND
        tax.taxonomy = 'category' )
    ORDER BY terms.term_id ASC
");
?>

<ul>
    <?php foreach($categories as $category) : ?>
<?php if ( ($category->ID == '3')   || ($category->ID == '4')  || ($category->ID == '5')) { ?>
    <li>
        <a href="<?php echo get_category_link( $category->ID ); ?>/?author_name=<?php echo $curuser->user_login; ?>" title="<?php echo $category->name ?>">
            <?php echo $category->name; ?>
        </a>
    </li>
<?php } ?>
    <?php endforeach; ?>
</ul>

Thank you all!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:32:33+00:00Added an answer on May 21, 2026 at 3:32 pm

    Judging by the looks of the wordpress database I have lying around, I’m guessing there is no index on the columns you are using in your WHERE clause for the wp_posts table.

    Try adding an index like this:

    ALTER TABLE wp_posts ADD INDEX (post_author,post_status).

    I bet you see a speed up from that.

    The best thing to do however is to run that query manually with an EXPLAIN in front of the SELECT, and analyze the output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following query is used for Getting Categories and one news for each category. How
The following query was successful when I used in mysql INSERT INTO user(`dev_id`,`email`) VALUES('123','456@gmail.com');
The following query used to grab all comments for a particular blog article and
How I can rewrite following query that I will take minus Correction and Used
I have used the following query in Silverstripe 2.x projects: $obj = DataObject::get_one('Post', \URLSegment\
I have the following query that I've used to pull out a vehicle ID,
I have written the following query, but in the WHERE clause I have used
The following code is used in a query for fetching records. It uses the
I'm using the following query to generate a unique new user: INSERT INTO `users`
I have the following query that when executed, it just runs for hours and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.