Skip to content

Codepolice

  • ⤫

Query the MySQL / WordPress Database from an ASP.NET site

Posted by Judy Alvarez Posted on February 28, 2022March 1, 2022
0

We run a community site on ASP.NET Webforms that also has a blog based on WordPress. For a couple of years now we have loaded the data from WordPress to the ASP.NET site via WordPress RSS Feeds but we have had issues with this for a long time.

Some days ago I decided to just query the MySQL database that WordPress uses directly from .NET instead of going via any WordPress API. The whole process was kind of straightforward.

Connection to MySQL from .Net

To just do a simple connection you just need the MySQL Nuget package then it works pretty much like any ADO.NET connection.

using (MySqlConnection myConnection = new MySqlConnection(ConfigurationManager.ConnectionStrings["mysql"].ToString())) {
     MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection);
     myConnection.Open();

     using (MySqlDataReader myReader = myCommand.ExecuteReader())                  {
      aListView.DataSource = myReader;
      aListView.DataBind();
   }
}

 Query the WordPress Database to get Posts by Category

I needed to get my posts by category and it was kind of simple.

SELECT DISTINCT posts.post_title, posts.post_content, posts.post_date, posts.post_name FROM wp_posts posts
 JOIN wp_term_relationships term_rel 
  ON posts.ID = term_rel.object_id
 JOIN wp_term_taxonomy term_tax 
  ON term_rel.term_taxonomy_id = term_tax.term_taxonomy_id
 JOIN wp_terms terms 
  ON term_tax.term_id = terms.term_id
WHERE taxonomy = 'category' 
 AND posts.post_type = 'post' 
 AND terms.slug != 'MyCategory' 
 AND posts.post_status = 'publish' 
ORDER BY posts.post_date desc LIMIT 15;

I also needed to get the “Attached Image” or “Featured Image” from WordPress.

SELECT DISTINCT posts.post_title, posts.post_content, posts.post_date, meta2.meta_value, posts.post_name 
FROM wp_posts posts
   JOIN wp_term_relationships term_rel 
     ON posts.ID = term_rel.object_id
   JOIN wp_term_taxonomy term_tax 
     ON term_rel.term_taxonomy_id = term_tax.term_taxonomy_id
   JOIN wp_terms terms 
     ON term_tax.term_id = terms.term_id
   JOIN wp_postmeta meta 
     ON (meta.post_id = posts.ID 
     AND meta.meta_value IS NOT NULL 
     AND meta.meta_key = '_thumbnail_id')
   JOIN wp_postmeta meta2 
     ON (meta2.post_id = meta.meta_value 
     AND meta2.meta_key = '_wp_attached_file' 
     AND meta2.meta_value IS NOT NULL)
WHERE taxonomy = 'category' 
 AND posts.post_type = 'post' 
 AND terms.slug = 'MyCategory' 
 AND posts.post_status = 'publish' 
ORDER BY posts.post_date desc
LIMIT 5";
Categories: JavascriptTagged: asp .net web hosting, asp net certification, asp net coding, asp net core 3.1 oauth2 example, asp net core architecture, asp net core development, asp net ecommerce open source, asp net example, asp net image, asp net impersonation, asp net interview questions for experienced, asp net jquery, asp net mvc angularjs, asp net mvc latest version, asp net mvc5, asp net o que é, asp net page events, asp net questions, asp net single page application, asp net template, asp net web forms interview questions, cms asp net, ecommerce asp net, gridview control in asp net, itvdn asp net core torrent, learn asp net core, login control in asp net, mvc asp net tutorial, routing in asp net core, w3schools asp net

Post navigation

Previous Previous post: Build, Git Commit, FTP When You Publish In Visual Studio
Next Next post: Problems with gzip when using IIS 7.5 as an Origin server for a CDN

Related Posts

  • Package efficiency and dependency hygiene

    #​752 — September 12, 2025 Read on the Web If you have any interest in music and being able to render music or generate music with JavaScript, be sure to check out the very end of this issue where we’ve dedicated an entire section to the topic 🙂__Your editor, Peter Cooper JavaScript Weekly How to Keep

    Posted by Posted on September 12, 2025
    0
  • A significant supply chain attack on the npm ecosystem

    #​592 — September 9, 2025 Read on the Web A Major Supply Chain Attack Hits the npm Ecosystem — In July, Socket warned us about a phishing campaign targeting npm package publishers. Sadly, a prolific package author (among others, like DuckDB, who explain how the attack worked on them) fell victim to the scam, resulting

    Posted by Posted on September 9, 2025
    0
  • Why browsers throttle JavaScript timers (and what to do about it)

    #​751 — September 5, 2025 Read on the Web JavaScript Weekly Mediabunny: A Complete Media Toolkit for JavaScript — Supporting both browsers and Node.js, this library lets you read, write and convert popular media file formats (e.g. MP4, MP3, and more) without needing to lean on dependencies like FFmpeg. You can make thumbnails, extract metadata,

    Posted by Posted on September 5, 2025
    0
  • The latest on Oracle’s hold on JavaScript

    #​750 — August 29, 2025 Read on the Web JavaScript Weekly An Illustrated Guide to Big O and Time Complexity — A fantastic JavaScript-oriented, interactive, visual essay about Big O notation and its role in describing algorithmic complexity. This is a beautiful bit of work, even if you’re already wise to O(log n) and O(n^2).

    Posted by Posted on August 29, 2025
    0
  • The productivity benefits from type stripping

    #​591 — August 26, 2025 Read on the Web How We Migrated Our Rush.js Monorepo to Node Type Stripping — Since v23.6 (and in LTS since v22.18.0), Node has supported running (most) TypeScript code by stripping the types out first. The Calm team was excited about the potential for improving productivity and DX, and set

    Posted by Posted on August 26, 2025
    0
  • We still love jQuery

    #​749 — August 22, 2025 Read on the Web ☀️ We’re back after a week off, though I’m starting to think we should have taken two weeks off as it’s been quite quiet in JavaScript-land this August! Nevertheless, we still have a full issue for you today, so let’s get on to it.. 😉__Peter Cooper, your

    Posted by Posted on August 22, 2025
    0
Judy Alvarez

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Codepolice

  • Github
  • Atlassian
  • Flatlogic
  • Xero
  • Jetbrains
  • Figma
  • Package efficiency and dependency hygiene
  • A significant supply chain attack on the npm ecosystem
  • Why browsers throttle JavaScript timers (and what to do about it)
  • The latest on Oracle’s hold on JavaScript
  • The productivity benefits from type stripping
https://flatlogic.com/generator
COPYRIGHT © 2025 - Codepolice