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

  • Building JavaScript tools in a single HTML file

    #​765 — December 12, 2025 Read on the Web JavaScript Weekly Useful Patterns for Building HTML Tools — In many situations, you don’t need a full-on framework to build useful tools; just HTML, JavaScript and CSS in a single file will do the job fine. Simon’s become a bit of an expert by rolling out

    Posted by Posted on December 12, 2025
    0
  • How the Seattle Times is using pnpm

    #​604 — December 9, 2025 Read on the Web 🗓️ A quick notice that Node Weekly will be moving to Thursdays in January 2026, as part of a schedule reshuffle for most of our newsletters. We still have one more week before the Christmas break, though, so we’ll be back next Tuesday with our 2025 roundup!__Your editor,

    Posted by Posted on December 10, 2025
    0
  • What’s the story? JavaScript’s 30!

    #​764 — December 5, 2025 Read on the Web JavaScript Weekly 🎉  JavaScript Turns 30 Years Old  🎉 Back in May 1995, a 33 year old Brendan Eich built the first prototype of JavaScript in just ten days, originally codenamed Mocha (and then LiveScript). On December 4, 1995, Netscape and Sun Microsystems officially announced ‘JavaScript’ in a

    Posted by Posted on December 5, 2025
    0
  • Comparing performance across Node versions and ARM vs x86

    #​603 — December 2, 2025 Read on the Web Tinybench 6.0: A Tiny, Simple Benchmarking Library — Uses whatever precise timing capabilities are available (e.g. process.hrtime or performance.now). You can then benchmark whatever functions you want, specify how long or how many times to benchmark for, and get a variety of stats in return –

    Posted by Posted on December 2, 2025
    0
  • Algorithms visualized and demonstrated in JavaScript

    #​763 — November 28, 2025 Read on the Web JavaScript Weekly Over 150 Algorithms and Data Structures Demonstrated in JS — Examples of many common algorithms (e.g. bit manipulation, Pascal’s triangle, Hamming distance) and data structures (e.g. linked lists, tries, graphs) with explanations. Available in eighteen other written languages too. Oleksii Trekhleb et al. TypeScript: From

    Posted by Posted on November 28, 2025
    0
  • Guess who’s back, back again? Shai-Hulud.

    #​602 — November 25, 2025 Read on the Web How a Summer in Abruzzo Helped Bring Type Stripping to Node.js — Node.js TSC member and committer Marco tells the personal tale of what it took to bring type stripping (now considered stable) to Node. It’s neat to get the back story. He’s now working on

    Posted by Posted on November 25, 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
  • Building JavaScript tools in a single HTML file
  • How the Seattle Times is using pnpm
  • What’s the story? JavaScript’s 30!
  • Comparing performance across Node versions and ARM vs x86
  • Algorithms visualized and demonstrated in JavaScript
https://flatlogic.com/generator
COPYRIGHT © 2025 - Codepolice