SlideShare uma empresa Scribd logo
1 de 26
Parsing Strange:URL to SQL to HTML Hal Stern snowmanonfire.comslideshare.net/freeholdhal headshot by Richard Stevenshttp://dieselsweeties.com
Why Do You Care? Database performance = user experience A little database expertise goes a long way Taxonomies for more than sidebar lists Custom post types WordPress as a powerful CMS >> blog Change default behaviors Defy the common wisdom Integrate other content sources/filters WordCamp NYC 2010 2
Flow of Control Web server URL manipulation Real file or permalink URL? URL to query variables What to display? Tag? Post? Category? Query variables to SQL generation How exactly to get that content? Template file selection How will content be displayed? Content manipulation 3 WordCamp NYC 2010
Whose File Is This? User URL request passed to web server Web server checks.htaccessfile WP install root  Other .htaccessfiles may interfere Basic rewriting rules:If file or directory URL doesn’t exist, start WordPress via index.php WordCamp NYC 2010 4 <IfModulemod_rewrite.c> RewriteEngine On RewriteBase /whereyouputWordPress/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
Example Meta Fail: 404 Not Found Access broken image URLs for unintended results: no 404 pages!myblog/images/not-a-pic.jpg Web server can’t find file, assumes it’s a permalink, hands to WP  WP can’t interpret it, so defaults to home WordCamp NYC 2010 5 myblog/ myblog/wp-content (etc) myblog/images
What Happens Before The Loop Parse URL into a query Set conditionals & select templates Execute the query & cache results Run the Loop:<?phpif (have_posts()) : 	   while (have_posts()) : the_post();    //loop contentendwhile;endif;?> WordCamp NYC 2010 6
Examining the Query String SQL passed to MySQL in WP_Query object’s request element Brute force: edit theme footer.phpto see main loop’s query for displayed page WordCamp NYC 2010 7 <?php   global $wp_query;   echo ”SQL for this page ";   echo $wp_query->request;   echo "<br>"; ?>
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post’ AND(wp_posts.post_status = 'publish' ORwp_posts.post_status = 'private’)ORDER BY wp_posts.post_date DESC LIMIT 0, 10 “Home Page” Query Deconstruction WordCamp NYC 2010 8 Get all fields from posts table, but limit number of returned rows Only get posts, and those that are published or private to the user Sort the results by date in descending order Start results starting with record 0 and up to 10 more results
Query Parsing parse_request() method of WP_Query extracts query variables from URL Execute rewrite rules Pick off ?p=67 style http GET variables Match permalink structure Match keywords like “author” and “tag” Match custom post type slugs WordCamp NYC 2010 9
Query Variables to SQL Query type: post by title, posts by category or tag, posts by date Variables for the query Slug values for category/tags Month/day numbers Explicit variable values post_typevariable has been around for a while; CPT queries fill in new values WordCamp NYC 2010 10
Simple Title Slug Parsing Rewrite matches root of permalink, extracts tail of URL as a title slug WordCamp NYC 2010 11 /2010/premio-sausage SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND YEAR(wp_posts.post_date)='2010' AND wp_posts.post_name = 'premio-sausage' AND wp_posts.post_type = 'post' ORDER BY wp_posts.post_date DESC
CPT Query Variables Register CPT with a custom query variable 'query_var' => 'ebay' Variable works in URLs like built-ins myblog.com/?ebay=current_items myblog.com/?ebay=cool_searches Variable value matches CPT title slug WordCamp NYC 2010 12
WordPress Meta Data Common DB mechanics for all meta data Categories, tags, custom taxonomies Normalized down to 3 tables Terms: word strings and their slugs Taxonomies: collections of terms Relationships: terms attached to posts It’s so simple it gets really complex.  Really. WordCamp NYC 2010 13
Graphs and JOIN Operations WordPress maps tags and categories 1:N to posts (each term in many posts) You need to punch MySQL to handle this INNER JOIN builds intermediate tables on common key values Following link in a graph is equivalent to an INNER JOIN on tables of linked items WordCamp NYC 2010 14
WordPress Taxonomy Tables Term relationships table maps N terms to each post Term taxonomy maps N terms to each taxonomy Term table has slugs for URL mapping WordCamp NYC 2010 15 wp_term_relationshipsobject_idterm_taxonomy_id wp_postspost_id….post_date…  post_content wp_term_taxonomyterm_taxonomy_idterm_idtaxonomydescription wp_terms term_idnameslug
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_postsINNER JOIN wp_term_relationships ON(wp_posts.ID = wp_term_relationships.object_id)INNER JOIN wp_term_taxonomy ON  (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)INNER JOIN wp_terms ON   (wp_term_taxonomy.term_id = wp_terms.term_id)WHERE 1=1 AND wp_term_taxonomy.taxonomy = 'post_tag' AND wp_terms.slug IN ('premio') AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10 Taxonomy Lookup WordCamp NYC 2010 16 /tag/premio
More on Canonical URLs Canonical URLs improve SEO WordPress is really good about generating 301 Redirects for non-standard URLs Example: URL doesn’t appear to match a permalink, WordPress does prediction Use “LIKE title%” in WHERE clause Matches “title” as initial substring with % wildcard WordCamp NYC 2010 17
Modifying the Query Brute force isn’t necessarily good Using query_posts() ignores all previous parsing, runs a new SQL query Filter query_vars Change default parsing (convert any day to a week’s worth of posts, for example) Actions parse_query & parse_request Access WP_Query object before execution is_xx() conditionals are already set WordCamp NYC 2010 18
SQL Generation Filters posts_where More explicit control over query variable to SQL grammar mapping posts_join Add or modify JOIN operations for other graph relationships Many other filters Change grouping of results Change ordering of results WordCamp NYC 2010 19
Custom Post Types Change SQL WHERE clause on post type wp_posts.post_type=‘ebay’ Add new rewrite rules for URL parsing similar to category & tag Set slug in CPT registration array'rewrite' => array ("slug" => “ebay”), Watch out for competing, overwritten or unflushed rewrite entries<?php echo "<pre>”;print_r(get_option('rewrite_rules'));echo "</pre>”;?> WordCamp NYC 2010 20
Applications Stylized listings Category sorted alphabetically Use posts as listings of resources (jobs, clients, events) – good CPT application Custom URL slugs Add rewrite rules to match slug and set query variables Joining other social graphs Suggested/related content WordCamp NYC 2010 21
Template File Selection is_x() conditionals set in query parsing Used to drive template selection is_tag() looks for tag-slug, tag-id, then tag Full search hierarchy in Codex template_redirectaction Called in the template loader Add actions to override defaults WordCamp NYC 2010 22
HTML Generation Done in the_post() method Raw content retrieved from MySQL Short codes interpreted CSS applied Some caching plugins generate and store HTML, so YMMV WordCamp NYC 2010 23
Why Do You Care? User experience improvement JOINS are expensive Large post table & repetitive SELECTs = slow Running query once keeps cache warm Category, permalink, title slug choices matter More CMS, less “blog” Alphabetical sort Adding taxonomy/social graph elements WordCamp NYC 2010 24
Resources Core files where SQL stuff happens query.php post.php canonical.php rewrite.php Template loader search path http://codex.wordpress.org/Template_Hierarchy WordCamp NYC 2010 25
Contact Hal Stern freeholdhal@gmail.com @freeholdhal snowmanonfire.com facebook.com/hal.stern Other Projects: amphibimen.com facebook.com/letusin slideshare.net/freeholdhal WordCamp NYC 2010 26

Mais conteúdo relacionado

Mais procurados

Intro to T-SQL – 2nd session
Intro to T-SQL – 2nd sessionIntro to T-SQL – 2nd session
Intro to T-SQL – 2nd sessionMedhat Dawoud
 
Xml part3
Xml part3Xml part3
Xml part3NOHA AW
 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developerAhsan Kabir
 
Itemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integrationItemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integration{item:foo}
 
SharePoint 2013 REST API & Remote Authentication
SharePoint 2013 REST API & Remote AuthenticationSharePoint 2013 REST API & Remote Authentication
SharePoint 2013 REST API & Remote AuthenticationAdil Ansari
 
Oracle basic queries
Oracle basic queriesOracle basic queries
Oracle basic queriesPRAKHAR JHA
 
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow TransformationsPramod Singla
 
PostgreSQL Tutorial For Beginners | Edureka
PostgreSQL Tutorial For Beginners | EdurekaPostgreSQL Tutorial For Beginners | Edureka
PostgreSQL Tutorial For Beginners | EdurekaEdureka!
 
Open Standards for the Semantic Web: XML / RDF(S) / OWL / SOAP
Open Standards for the Semantic Web: XML / RDF(S) / OWL / SOAPOpen Standards for the Semantic Web: XML / RDF(S) / OWL / SOAP
Open Standards for the Semantic Web: XML / RDF(S) / OWL / SOAPPieter De Leenheer
 

Mais procurados (14)

Schema201 webinar
Schema201 webinarSchema201 webinar
Schema201 webinar
 
Intro to T-SQL – 2nd session
Intro to T-SQL – 2nd sessionIntro to T-SQL – 2nd session
Intro to T-SQL – 2nd session
 
Xml part3
Xml part3Xml part3
Xml part3
 
Steps towards of sql server developer
Steps towards of sql server developerSteps towards of sql server developer
Steps towards of sql server developer
 
Itemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integrationItemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integration
 
T-SQL Overview
T-SQL OverviewT-SQL Overview
T-SQL Overview
 
SQL for ETL Testing
SQL for ETL TestingSQL for ETL Testing
SQL for ETL Testing
 
SharePoint 2013 REST API & Remote Authentication
SharePoint 2013 REST API & Remote AuthenticationSharePoint 2013 REST API & Remote Authentication
SharePoint 2013 REST API & Remote Authentication
 
Ado.Net
Ado.NetAdo.Net
Ado.Net
 
Oracle basic queries
Oracle basic queriesOracle basic queries
Oracle basic queries
 
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
 
Html tags describe in bangla
Html tags describe in banglaHtml tags describe in bangla
Html tags describe in bangla
 
PostgreSQL Tutorial For Beginners | Edureka
PostgreSQL Tutorial For Beginners | EdurekaPostgreSQL Tutorial For Beginners | Edureka
PostgreSQL Tutorial For Beginners | Edureka
 
Open Standards for the Semantic Web: XML / RDF(S) / OWL / SOAP
Open Standards for the Semantic Web: XML / RDF(S) / OWL / SOAPOpen Standards for the Semantic Web: XML / RDF(S) / OWL / SOAP
Open Standards for the Semantic Web: XML / RDF(S) / OWL / SOAP
 

Destaque

Parsing strange v4
Parsing strange v4Parsing strange v4
Parsing strange v4Hal Stern
 
Parsing strange v2
Parsing strange v2Parsing strange v2
Parsing strange v2Hal Stern
 
Parsing strange v1.1
Parsing strange v1.1Parsing strange v1.1
Parsing strange v1.1Hal Stern
 
Parsing strange v4
Parsing strange v4Parsing strange v4
Parsing strange v4Hal Stern
 
Ogra S1 2010 Op Competitiveness Part1
Ogra S1 2010 Op Competitiveness Part1Ogra S1 2010 Op Competitiveness Part1
Ogra S1 2010 Op Competitiveness Part1Veselina Nikolova
 
Stephen 205 (1)
Stephen 205 (1)Stephen 205 (1)
Stephen 205 (1)farsiya
 
Stephen Rathinaraj Corr
Stephen Rathinaraj   CorrStephen Rathinaraj   Corr
Stephen Rathinaraj Corrfarsiya
 
Shades team in action
Shades team in actionShades team in action
Shades team in actionDonna Roberts
 
Entr sme10 the_secret_of_success_-_the_many_faces_of_european_entrepreneurshi...
Entr sme10 the_secret_of_success_-_the_many_faces_of_european_entrepreneurshi...Entr sme10 the_secret_of_success_-_the_many_faces_of_european_entrepreneurshi...
Entr sme10 the_secret_of_success_-_the_many_faces_of_european_entrepreneurshi...Veselina Nikolova
 
Teoria de control presentacion
Teoria de control presentacionTeoria de control presentacion
Teoria de control presentacioncesar
 
100512074 analisis-de-respuesta-transitoria-docx
100512074 analisis-de-respuesta-transitoria-docx100512074 analisis-de-respuesta-transitoria-docx
100512074 analisis-de-respuesta-transitoria-docxcesar
 

Destaque (17)

Parsing strange v4
Parsing strange v4Parsing strange v4
Parsing strange v4
 
Parsing strange v2
Parsing strange v2Parsing strange v2
Parsing strange v2
 
Presentation sofia
Presentation sofiaPresentation sofia
Presentation sofia
 
Parsing strange v1.1
Parsing strange v1.1Parsing strange v1.1
Parsing strange v1.1
 
Ogra s3-2010 success
Ogra s3-2010 successOgra s3-2010 success
Ogra s3-2010 success
 
Parsing strange v4
Parsing strange v4Parsing strange v4
Parsing strange v4
 
ЕГДБ
ЕГДБЕГДБ
ЕГДБ
 
Faces all
Faces allFaces all
Faces all
 
11 13
11 1311 13
11 13
 
Ogra S1 2010 Op Competitiveness Part1
Ogra S1 2010 Op Competitiveness Part1Ogra S1 2010 Op Competitiveness Part1
Ogra S1 2010 Op Competitiveness Part1
 
Ogra s4-2010 tenders
Ogra s4-2010 tendersOgra s4-2010 tenders
Ogra s4-2010 tenders
 
Stephen 205 (1)
Stephen 205 (1)Stephen 205 (1)
Stephen 205 (1)
 
Stephen Rathinaraj Corr
Stephen Rathinaraj   CorrStephen Rathinaraj   Corr
Stephen Rathinaraj Corr
 
Shades team in action
Shades team in actionShades team in action
Shades team in action
 
Entr sme10 the_secret_of_success_-_the_many_faces_of_european_entrepreneurshi...
Entr sme10 the_secret_of_success_-_the_many_faces_of_european_entrepreneurshi...Entr sme10 the_secret_of_success_-_the_many_faces_of_european_entrepreneurshi...
Entr sme10 the_secret_of_success_-_the_many_faces_of_european_entrepreneurshi...
 
Teoria de control presentacion
Teoria de control presentacionTeoria de control presentacion
Teoria de control presentacion
 
100512074 analisis-de-respuesta-transitoria-docx
100512074 analisis-de-respuesta-transitoria-docx100512074 analisis-de-respuesta-transitoria-docx
100512074 analisis-de-respuesta-transitoria-docx
 

Semelhante a Parsing strange v3

Parsing strange v3
Parsing strange v3Parsing strange v3
Parsing strange v3Hal Stern
 
Assignment 2 - Power drill Grapevine "It's like Yik Yak, but for opinions ab...
Assignment 2 - Power drill Grapevine  "It's like Yik Yak, but for opinions ab...Assignment 2 - Power drill Grapevine  "It's like Yik Yak, but for opinions ab...
Assignment 2 - Power drill Grapevine "It's like Yik Yak, but for opinions ab...MATCHmaster
 
Wss Object Model
Wss Object ModelWss Object Model
Wss Object Modelmaddinapudi
 
Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011David Carr
 
Exploring Symfony's Code
Exploring Symfony's CodeExploring Symfony's Code
Exploring Symfony's CodeWildan Maulana
 
Sun certifiedwebcomponentdeveloperstudyguide
Sun certifiedwebcomponentdeveloperstudyguideSun certifiedwebcomponentdeveloperstudyguide
Sun certifiedwebcomponentdeveloperstudyguideAlberto Romero Jiménez
 
ASP.NET MVC introduction
ASP.NET MVC introductionASP.NET MVC introduction
ASP.NET MVC introductionTomi Juhola
 
WordPress Structure and Best Practices
WordPress Structure and Best PracticesWordPress Structure and Best Practices
WordPress Structure and Best Practicesmarkparolisi
 
SQL for Web APIs - Simplifying Data Access for API Consumers
SQL for Web APIs - Simplifying Data Access for API ConsumersSQL for Web APIs - Simplifying Data Access for API Consumers
SQL for Web APIs - Simplifying Data Access for API ConsumersJerod Johnson
 
Building social and RESTful frameworks
Building social and RESTful frameworksBuilding social and RESTful frameworks
Building social and RESTful frameworksbrendonschwartz
 
Introduction to SQLite in Adobe AIR
Introduction to SQLite in Adobe AIRIntroduction to SQLite in Adobe AIR
Introduction to SQLite in Adobe AIRPeter Elst
 
Wcf data services
Wcf data servicesWcf data services
Wcf data servicesEyal Vardi
 
SQLite in Adobe AIR
SQLite in Adobe AIRSQLite in Adobe AIR
SQLite in Adobe AIRPeter Elst
 
Word2vec in Postgres
Word2vec in PostgresWord2vec in Postgres
Word2vec in PostgresGary Sieling
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalystdwm042
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)Talha Ocakçı
 
Usability AJAX and other ASP.NET Features
Usability AJAX and other ASP.NET FeaturesUsability AJAX and other ASP.NET Features
Usability AJAX and other ASP.NET FeaturesPeter Gfader
 

Semelhante a Parsing strange v3 (20)

Parsing strange v3
Parsing strange v3Parsing strange v3
Parsing strange v3
 
Assignment 2 - Power drill Grapevine "It's like Yik Yak, but for opinions ab...
Assignment 2 - Power drill Grapevine  "It's like Yik Yak, but for opinions ab...Assignment 2 - Power drill Grapevine  "It's like Yik Yak, but for opinions ab...
Assignment 2 - Power drill Grapevine "It's like Yik Yak, but for opinions ab...
 
Wss Object Model
Wss Object ModelWss Object Model
Wss Object Model
 
Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011
 
Practical OData
Practical ODataPractical OData
Practical OData
 
Exploring Symfony's Code
Exploring Symfony's CodeExploring Symfony's Code
Exploring Symfony's Code
 
Sun certifiedwebcomponentdeveloperstudyguide
Sun certifiedwebcomponentdeveloperstudyguideSun certifiedwebcomponentdeveloperstudyguide
Sun certifiedwebcomponentdeveloperstudyguide
 
ASP.NET MVC introduction
ASP.NET MVC introductionASP.NET MVC introduction
ASP.NET MVC introduction
 
WordPress Structure and Best Practices
WordPress Structure and Best PracticesWordPress Structure and Best Practices
WordPress Structure and Best Practices
 
SQL for Web APIs - Simplifying Data Access for API Consumers
SQL for Web APIs - Simplifying Data Access for API ConsumersSQL for Web APIs - Simplifying Data Access for API Consumers
SQL for Web APIs - Simplifying Data Access for API Consumers
 
Building social and RESTful frameworks
Building social and RESTful frameworksBuilding social and RESTful frameworks
Building social and RESTful frameworks
 
Introduction to SQLite in Adobe AIR
Introduction to SQLite in Adobe AIRIntroduction to SQLite in Adobe AIR
Introduction to SQLite in Adobe AIR
 
Wcf data services
Wcf data servicesWcf data services
Wcf data services
 
PPT
PPTPPT
PPT
 
SQLite in Adobe AIR
SQLite in Adobe AIRSQLite in Adobe AIR
SQLite in Adobe AIR
 
Lecture13
Lecture13Lecture13
Lecture13
 
Word2vec in Postgres
Word2vec in PostgresWord2vec in Postgres
Word2vec in Postgres
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)
 
Usability AJAX and other ASP.NET Features
Usability AJAX and other ASP.NET FeaturesUsability AJAX and other ASP.NET Features
Usability AJAX and other ASP.NET Features
 

Parsing strange v3

  • 1. Parsing Strange:URL to SQL to HTML Hal Stern snowmanonfire.comslideshare.net/freeholdhal headshot by Richard Stevenshttp://dieselsweeties.com
  • 2. Why Do You Care? Database performance = user experience A little database expertise goes a long way Taxonomies for more than sidebar lists Custom post types WordPress as a powerful CMS >> blog Change default behaviors Defy the common wisdom Integrate other content sources/filters WordCamp NYC 2010 2
  • 3. Flow of Control Web server URL manipulation Real file or permalink URL? URL to query variables What to display? Tag? Post? Category? Query variables to SQL generation How exactly to get that content? Template file selection How will content be displayed? Content manipulation 3 WordCamp NYC 2010
  • 4. Whose File Is This? User URL request passed to web server Web server checks.htaccessfile WP install root Other .htaccessfiles may interfere Basic rewriting rules:If file or directory URL doesn’t exist, start WordPress via index.php WordCamp NYC 2010 4 <IfModulemod_rewrite.c> RewriteEngine On RewriteBase /whereyouputWordPress/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
  • 5. Example Meta Fail: 404 Not Found Access broken image URLs for unintended results: no 404 pages!myblog/images/not-a-pic.jpg Web server can’t find file, assumes it’s a permalink, hands to WP WP can’t interpret it, so defaults to home WordCamp NYC 2010 5 myblog/ myblog/wp-content (etc) myblog/images
  • 6. What Happens Before The Loop Parse URL into a query Set conditionals & select templates Execute the query & cache results Run the Loop:<?phpif (have_posts()) : while (have_posts()) : the_post(); //loop contentendwhile;endif;?> WordCamp NYC 2010 6
  • 7. Examining the Query String SQL passed to MySQL in WP_Query object’s request element Brute force: edit theme footer.phpto see main loop’s query for displayed page WordCamp NYC 2010 7 <?php global $wp_query; echo ”SQL for this page "; echo $wp_query->request; echo "<br>"; ?>
  • 8. SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post’ AND(wp_posts.post_status = 'publish' ORwp_posts.post_status = 'private’)ORDER BY wp_posts.post_date DESC LIMIT 0, 10 “Home Page” Query Deconstruction WordCamp NYC 2010 8 Get all fields from posts table, but limit number of returned rows Only get posts, and those that are published or private to the user Sort the results by date in descending order Start results starting with record 0 and up to 10 more results
  • 9. Query Parsing parse_request() method of WP_Query extracts query variables from URL Execute rewrite rules Pick off ?p=67 style http GET variables Match permalink structure Match keywords like “author” and “tag” Match custom post type slugs WordCamp NYC 2010 9
  • 10. Query Variables to SQL Query type: post by title, posts by category or tag, posts by date Variables for the query Slug values for category/tags Month/day numbers Explicit variable values post_typevariable has been around for a while; CPT queries fill in new values WordCamp NYC 2010 10
  • 11. Simple Title Slug Parsing Rewrite matches root of permalink, extracts tail of URL as a title slug WordCamp NYC 2010 11 /2010/premio-sausage SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND YEAR(wp_posts.post_date)='2010' AND wp_posts.post_name = 'premio-sausage' AND wp_posts.post_type = 'post' ORDER BY wp_posts.post_date DESC
  • 12. CPT Query Variables Register CPT with a custom query variable 'query_var' => 'ebay' Variable works in URLs like built-ins myblog.com/?ebay=current_items myblog.com/?ebay=cool_searches Variable value matches CPT title slug WordCamp NYC 2010 12
  • 13. WordPress Meta Data Common DB mechanics for all meta data Categories, tags, custom taxonomies Normalized down to 3 tables Terms: word strings and their slugs Taxonomies: collections of terms Relationships: terms attached to posts It’s so simple it gets really complex. Really. WordCamp NYC 2010 13
  • 14. Graphs and JOIN Operations WordPress maps tags and categories 1:N to posts (each term in many posts) You need to punch MySQL to handle this INNER JOIN builds intermediate tables on common key values Following link in a graph is equivalent to an INNER JOIN on tables of linked items WordCamp NYC 2010 14
  • 15. WordPress Taxonomy Tables Term relationships table maps N terms to each post Term taxonomy maps N terms to each taxonomy Term table has slugs for URL mapping WordCamp NYC 2010 15 wp_term_relationshipsobject_idterm_taxonomy_id wp_postspost_id….post_date… post_content wp_term_taxonomyterm_taxonomy_idterm_idtaxonomydescription wp_terms term_idnameslug
  • 16. SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_postsINNER JOIN wp_term_relationships ON(wp_posts.ID = wp_term_relationships.object_id)INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)INNER JOIN wp_terms ON (wp_term_taxonomy.term_id = wp_terms.term_id)WHERE 1=1 AND wp_term_taxonomy.taxonomy = 'post_tag' AND wp_terms.slug IN ('premio') AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10 Taxonomy Lookup WordCamp NYC 2010 16 /tag/premio
  • 17. More on Canonical URLs Canonical URLs improve SEO WordPress is really good about generating 301 Redirects for non-standard URLs Example: URL doesn’t appear to match a permalink, WordPress does prediction Use “LIKE title%” in WHERE clause Matches “title” as initial substring with % wildcard WordCamp NYC 2010 17
  • 18. Modifying the Query Brute force isn’t necessarily good Using query_posts() ignores all previous parsing, runs a new SQL query Filter query_vars Change default parsing (convert any day to a week’s worth of posts, for example) Actions parse_query & parse_request Access WP_Query object before execution is_xx() conditionals are already set WordCamp NYC 2010 18
  • 19. SQL Generation Filters posts_where More explicit control over query variable to SQL grammar mapping posts_join Add or modify JOIN operations for other graph relationships Many other filters Change grouping of results Change ordering of results WordCamp NYC 2010 19
  • 20. Custom Post Types Change SQL WHERE clause on post type wp_posts.post_type=‘ebay’ Add new rewrite rules for URL parsing similar to category & tag Set slug in CPT registration array'rewrite' => array ("slug" => “ebay”), Watch out for competing, overwritten or unflushed rewrite entries<?php echo "<pre>”;print_r(get_option('rewrite_rules'));echo "</pre>”;?> WordCamp NYC 2010 20
  • 21. Applications Stylized listings Category sorted alphabetically Use posts as listings of resources (jobs, clients, events) – good CPT application Custom URL slugs Add rewrite rules to match slug and set query variables Joining other social graphs Suggested/related content WordCamp NYC 2010 21
  • 22. Template File Selection is_x() conditionals set in query parsing Used to drive template selection is_tag() looks for tag-slug, tag-id, then tag Full search hierarchy in Codex template_redirectaction Called in the template loader Add actions to override defaults WordCamp NYC 2010 22
  • 23. HTML Generation Done in the_post() method Raw content retrieved from MySQL Short codes interpreted CSS applied Some caching plugins generate and store HTML, so YMMV WordCamp NYC 2010 23
  • 24. Why Do You Care? User experience improvement JOINS are expensive Large post table & repetitive SELECTs = slow Running query once keeps cache warm Category, permalink, title slug choices matter More CMS, less “blog” Alphabetical sort Adding taxonomy/social graph elements WordCamp NYC 2010 24
  • 25. Resources Core files where SQL stuff happens query.php post.php canonical.php rewrite.php Template loader search path http://codex.wordpress.org/Template_Hierarchy WordCamp NYC 2010 25
  • 26. Contact Hal Stern freeholdhal@gmail.com @freeholdhal snowmanonfire.com facebook.com/hal.stern Other Projects: amphibimen.com facebook.com/letusin slideshare.net/freeholdhal WordCamp NYC 2010 26

Notas do Editor

  1. yesterday – UX, URLs are your currencyit’s how the world sees us, and gives us incredible flexibility to direct/guide users
  2. SQL_CALC_FOUND_ROWS limits the number of returned rows via the LIMIT clause, and ensures that you don’t tax MySQL,Perform immense queries (SMOF has 600 post entries)WHERE 1=1 is for building compound where clauses; ensures there’s no degenerate caseType=post versus revision; status publish/private versus draft, trash
  3. Look at rewrite.php, and canonical.php (more on that later)Default terms of “tag” and “category” can be changed in the Settings/Permalinks section of the Dashboard
  4. You can
  5. Separate namespaces for pages and postsWhat about parent pages?In this example the permalink structure is %year%/%title%
  6. Three joins needed to build the full cartesian product of related tables.Get all of the terms that have a slug of “premio”, and find out what taxonomies they’re inGet the taxonomies that are post tags, and find all taxonomy object ids (that are post tags of slug “premio”)Get all of the posts that have this object id associated with them from term_relationshipsOrder the final table by post date,starting with the most recent (0) and getting 10 of them.
  7. Don’t want multiple URLs pointing to the same page, so canonical parsing cleans them up