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

  • Babel 8 RC Arrives, Gatsby Lives, Lodash Resets

    #​771 — February 3, 2026 Read on the Web JavaScript Weekly Four Heavyweights Drop Updates Four stalwarts of the JavaScript ecosystem all shipped notable releases this week, and odds are you’re using at least one of them: Gatsby v5.16 proves Gatsby, once considered neck-and-neck with Next.js in the React world, is not ‘dead’. The headline

    Posted by Posted on February 3, 2026
    0
  • A smoother way to ship Node apps

    #​609 — January 29, 2026 Read on the Web 🌊 Improving Single Executable Application Building in Node — First introduced two years ago, Node has a (still experimental) feature to build single executable applications that can be deployed to machines that don’t have Node installed. This week’s Node.js 25.5 release, with its –build-sea flag, moves the

    Posted by Posted on January 29, 2026
    0
  • What’s next for JavaScript frameworks in 2026

    #​770 — January 27, 2026 Read on the Web JavaScript Weekly Introducing LibPDF: PDF Parsing and Generation from TypeScript — LibPDF bills itself as ‘the PDF library TypeScript deserves’ and supports parsing, modifying, signing and generating PDFs with a modern API in Node, Bun, and the browser. GitHub repo. Documenso JavaScript Frameworks – Heading into 2026

    Posted by Posted on January 27, 2026
    0
  • require(esm) now stable in Node 25

    #​608 — January 22, 2026 Read on the Web Node.js 25.4.0 (Current) Released — Another gradual step forward for Node with require(esm) now marked as stable, as well as the module compile cache, along with a variety of other minor tweaks. Joyee Cheung of the Node team has written a thread on Bluesky going deeper

    Posted by Posted on January 22, 2026
    0
  • A big week for jQuery

    #​769 — January 20, 2026 Read on the Web JavaScript Weekly jQuery 4.0 Released — 20 years on from its original release, the ever-popular (in terms of actual usage) library reaches 4.0 with a migration to ES modules (compatible with modern build tools) along with dropping support for IE 10 and older. With jQuery being

    Posted by Posted on January 20, 2026
    0
  • A new guide to configuring Node packages

    #​607 — January 15, 2026 Read on the Web ⚠️ The Node.js January 13, 2026 Security Releases — Originally expected in December, these releases (of Node.js 25.3.0, 24.13.0, 22.22.0, and 20.20.0) finally landed this week, largely due to their complexity and the scope of the vulnerabilities they tackle. More on that in the next item! The Node.js

    Posted by Posted on January 15, 2026
    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
  • Babel 8 RC Arrives, Gatsby Lives, Lodash Resets
  • A smoother way to ship Node apps
  • What’s next for JavaScript frameworks in 2026
  • require(esm) now stable in Node 25
  • A big week for jQuery
https://flatlogic.com/generator
COPYRIGHT © 2026 - Codepolice