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

  • TypeScript and JavaScript dominate on GitHub in 2025

    #​759 — October 31, 2025 Read on the Web JavaScript Weekly Directives and the Platform Boundary — First there was the “use strict” directive to opt in to strict mode in JavaScript, but now you’ll encounter use client, use server, React’s new use no memo, and more, and they’re not standard JS features at all.

    Posted by Posted on October 31, 2025
    0
  • The reality of porting Deno code to Node

    #​598 — October 28, 2025 Read on the Web Awesome Node: Over 500 Curated Packages, Resources and Links — It’s been more than four years since we linked to Sindre’s handy resource, but it continues to get updates and tweaks (and, if you want, you can contribute a submission too – though the bar is

    Posted by Posted on October 28, 2025
    0
  • Vitest 4.0 and Next.js 16

    #​758 — October 24, 2025 Read on the Web JavaScript Weekly Vitest 4.0 Released: The Vite-Native Testing Framework — The Vite-powered, Jest-compatible testing framework introduces visual regression testing, makes its ‘Browser Mode’ stable (for running tests in a browser directly), adds Playwright Traces support, and more. Still unsure? You can compare it with other test

    Posted by Posted on October 24, 2025
    0
  • Node.js 25.0 arrives

    #​597 — October 21, 2025 Read on the Web Node.js v25.0.0 (Current) Released — The latest cutting edge version of Node has arrived with Web Storage enabled by default, JSON.stringify perf improvements, a new –allow-net option in the permission model, built-in Uint8Array base64/hex conversion, and WebAssembly and JIT optimizations. As shown in the diagram above, this

    Posted by Posted on October 21, 2025
    0
  • A Bun-believable release that isn’t half-baked

    #​757 — October 17, 2025 Read on the Web JavaScript Weekly Bun 1.3: The Full-Stack JavaScript Runtime — Arriving a few hours after last week’s issue (natch!) Bun 1.3 remains the big news of the past week. Bun is a performance and DX-focused JavaScriptCore-powered runtime which, with v1.3, balances being a drop-in Node.js replacement with

    Posted by Posted on October 17, 2025
    0
  • Bringing Python apps into Node

    #​596 — October 14, 2025 Read on the Web 📂 A Modern Guide to Reading and Writing Files in Node — A comprehensive guide to various methods for working with files, from promise-based methods through to working with streams, processing files concurrently, using file handles, and memory-efficient techniques. Luciano Mammino A Way to Integrate Python ASGI with Node.js

    Posted by Posted on October 14, 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
  • TypeScript and JavaScript dominate on GitHub in 2025
  • The reality of porting Deno code to Node
  • Vitest 4.0 and Next.js 16
  • Node.js 25.0 arrives
  • A Bun-believable release that isn’t half-baked
https://flatlogic.com/generator
COPYRIGHT © 2025 - Codepolice