{"id":1513,"date":"2022-03-14T22:43:15","date_gmt":"2022-03-14T17:13:15","guid":{"rendered":"https:\/\/myfreeonlinetools.com\/blog\/?p=1513"},"modified":"2022-03-14T22:43:15","modified_gmt":"2022-03-14T17:13:15","slug":"with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api","status":"publish","type":"post","link":"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/","title":{"rendered":"With PHP MySQL Query retrieve fetch all WordPress Blog categories in JSON REST API"},"content":{"rendered":"<h2>PHP MySQL database query for fetching categories data in JSON Format.<\/h2>\n<p>&nbsp;<\/p>\n<p>I run my WordPress website with MySQL database so need to fetch the database categories post data in JSON format and use it on another website created with REST API. In the below code, I will provide MySQL queries that can run on the WordPress MySQL database to get a list of published posts in the published category. Also, include the $_GET method key for anyone who can not access the code or hit unnecessary hits on databases.<\/p>\n<p>&nbsp;<\/p>\n<p>Get all categories id, post title, name, and image path in JSON format code with PHP and MySql query.<\/p>\n<h3>Below MySql query for retrieving categories from wp_posts, wp_terms, wp_postmeta.<\/h3>\n<pre>&lt;?php\r\nheader('Access-Control-Allow-Origin: *');\r\nheader('Access-Control-Allow-Methods: GET, POST, DELETE, HEAD, PUT');\r\nheader(\"Access-Control-Allow-Headers: X-Requested-With\");\r\n\r\n$key = $_GET['key'];\r\nif(empty($key) || ($key != '8edca156-080b-4aa1-884b-11f15d8tygva156')){\r\n\t$response=array(\r\n\t\t\t\t'status' =&gt; 0,\r\n\t\t\t\t'status_message' =&gt;'Access Denied.'\r\n\t\t\t);\r\n\t\theader('Content-Type: application\/json');\r\n\t\techo json_encode($response);\r\n\t\t\texit;\r\n}\r\n\/\/ Database configuration\r\n\r\n$dbHost     = \"localhost\";\r\n$dbUsername = \"XXXXXXXXXXXX\";\r\n$dbPassword = \"XXXXXXXXXXXX\";\r\n$dbName     = \"XXXXXXXXXXXX\";\r\n\r\n$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);\r\nif ($db -&gt; connect_errno) {\r\n  echo \"Failed to connect to MySQL: \" . $db -&gt; connect_error;\r\n  exit();\r\n}\r\n\t\r\n$request_method=$_SERVER[\"REQUEST_METHOD\"];\r\n\tswitch($request_method)\r\n\t{\r\n\t\tcase 'GET':\r\n\t\t\t\/\/ Retrive Post\r\n\t\t\tif(!empty($_GET[\"id\"]))\r\n\t\t\t{\r\n\t\t\t\t$product_id=intval($_GET[\"id\"]);\r\n\t\t\t\tget_post($product_id);\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tget_post();\r\n\t\t\t}\r\n\t\t\tbreak;\r\n\t\tdefault:\r\n\t\t\t\/\/ Invalid Request Method\r\n\t\t\theader(\"HTTP\/1.0 405 Method Not Allowed\");\r\n\t\t\tbreak;\r\n\t}\r\n    \r\n    function get_post()\r\n\t{\r\n\t\tglobal $db;\r\n          \r\n          $getCategary = $_POST['categaryValue'];         \r\n            echo $getCategary;\r\n        if($getCategary == ''){\r\n            $getCategary = 'News';\r\n        }\r\n        \r\n     \/\/   echo $getCategary;\r\n          \r\n\t\t\t$query = $db-&gt;query(\"SELECT wp_posts.id,\r\n       wp_posts.post_title,\r\n       wp_terms.name,\r\n       (SELECT guid\r\n        FROM   wp_posts\r\n        WHERE  id = wp_postmeta.meta_value) AS image\r\nFROM   wp_posts,\r\n       wp_postmeta,\r\n       wp_term_relationships,\r\n       wp_terms\r\nWHERE  wp_posts.id = wp_term_relationships.object_id\r\n       AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id\r\n       AND wp_posts.post_status = 'publish'\r\n       AND wp_posts.post_type = 'post'\r\n       AND wp_postmeta.post_id = wp_posts.id\r\n       AND wp_postmeta.meta_key = '_thumbnail_id'\r\nORDER  BY wp_posts.post_date DESC\r\nLIMIT  25\");\r\n        \t\r\n\t\t$response=array();\r\n\t\t\t\t\r\n\t\twhile($row = $query-&gt;fetch_assoc())\r\n\t\t{\r\n\t\t\t$remove[] = \"'\";\r\n\t\t\t$remove[] = '\"';\r\n\t\t\t\/\/$remove[] = '?';\r\n\t\t\t$remove[] = chr(054);\r\n\t\t\t$response[] = str_replace( $remove, \"\", $row );\r\n\t\t\t\/\/$response[] = mb_convert_encoding($row,'HTML-ENTITIES','UTF-8');\r\n\t\t}\r\n\t\t\r\n\t\theader('Content-type: application\/json; charset=utf-8');\r\n\t\techo json_encode($response);\r\n\t\t\r\n}\r\n?&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP MySQL database query for fetching categories data in JSON Format. &nbsp; I run my WordPress website with MySQL database so need to fetch the database categories post data in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[46,29],"tags":[],"class_list":["post-1513","post","type-post","status-publish","format-standard","hentry","category-mysql","category-php"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>With PHP MySQL Query retrieve fetch all WordPress Blog categories in JSON REST API - MyFreeOnlineTools<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"With PHP MySQL Query retrieve fetch all WordPress Blog categories in JSON REST API - MyFreeOnlineTools\" \/>\n<meta property=\"og:description\" content=\"PHP MySQL database query for fetching categories data in JSON Format. &nbsp; I run my WordPress website with MySQL database so need to fetch the database categories post data in [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/\" \/>\n<meta property=\"og:site_name\" content=\"MyFreeOnlineTools\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/LearnSchoolOnline\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-14T17:13:15+00:00\" \/>\n<meta name=\"author\" content=\"myfreeonlinetools\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"myfreeonlinetools\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/\"},\"author\":{\"name\":\"myfreeonlinetools\",\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/#\/schema\/person\/b1eb72e57c554e3b33cfeec477efcc3c\"},\"headline\":\"With PHP MySQL Query retrieve fetch all WordPress Blog categories in JSON REST API\",\"datePublished\":\"2022-03-14T17:13:15+00:00\",\"dateModified\":\"2022-03-14T17:13:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/\"},\"wordCount\":138,\"publisher\":{\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/#organization\"},\"articleSection\":[\"MySQL\",\"PHP\"],\"inLanguage\":\"en\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/\",\"url\":\"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/\",\"name\":\"With PHP MySQL Query retrieve fetch all WordPress Blog categories in JSON REST API - MyFreeOnlineTools\",\"isPartOf\":{\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/#website\"},\"datePublished\":\"2022-03-14T17:13:15+00:00\",\"dateModified\":\"2022-03-14T17:13:15+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/#breadcrumb\"},\"inLanguage\":\"en\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/myfreeonlinetools.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"With PHP MySQL Query retrieve fetch all WordPress Blog categories in JSON REST API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/#website\",\"url\":\"https:\/\/myfreeonlinetools.com\/blog\/\",\"name\":\"MyFreeOnlineTools\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/myfreeonlinetools.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/#organization\",\"name\":\"MyFreeOnlineTools\",\"url\":\"https:\/\/myfreeonlinetools.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en\",\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/myfreeonlinetools.com\/blog\/wp-content\/uploads\/2019\/11\/myfreeonlinetools-blue.png\",\"contentUrl\":\"https:\/\/myfreeonlinetools.com\/blog\/wp-content\/uploads\/2019\/11\/myfreeonlinetools-blue.png\",\"width\":387,\"height\":79,\"caption\":\"MyFreeOnlineTools\"},\"image\":{\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/#\/schema\/person\/b1eb72e57c554e3b33cfeec477efcc3c\",\"name\":\"myfreeonlinetools\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en\",\"@id\":\"https:\/\/myfreeonlinetools.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b870b17c6c7e3b75d7fe0b8bebfc9cf5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b870b17c6c7e3b75d7fe0b8bebfc9cf5?s=96&d=mm&r=g\",\"caption\":\"myfreeonlinetools\"},\"description\":\"Live in Delhi, Working in Gurgaon as Web Designer and Graphic Designer. Developed and Design myfreeonlinetools for online free tools. Also having youtube channel Name with LearnSchoolOnline. Traveling, watching movies, coding are the hobbies.\",\"sameAs\":[\"https:\/\/myfreeonlinetools.com\/\",\"https:\/\/www.facebook.com\/LearnSchoolOnline\/\",\"https:\/\/www.youtube.com\/learnschoolonline\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"With PHP MySQL Query retrieve fetch all WordPress Blog categories in JSON REST API - MyFreeOnlineTools","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/","og_locale":"en_US","og_type":"article","og_title":"With PHP MySQL Query retrieve fetch all WordPress Blog categories in JSON REST API - MyFreeOnlineTools","og_description":"PHP MySQL database query for fetching categories data in JSON Format. &nbsp; I run my WordPress website with MySQL database so need to fetch the database categories post data in [&hellip;]","og_url":"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/","og_site_name":"MyFreeOnlineTools","article_author":"https:\/\/www.facebook.com\/LearnSchoolOnline\/","article_published_time":"2022-03-14T17:13:15+00:00","author":"myfreeonlinetools","twitter_card":"summary_large_image","twitter_misc":{"Written by":"myfreeonlinetools","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/#article","isPartOf":{"@id":"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/"},"author":{"name":"myfreeonlinetools","@id":"https:\/\/myfreeonlinetools.com\/blog\/#\/schema\/person\/b1eb72e57c554e3b33cfeec477efcc3c"},"headline":"With PHP MySQL Query retrieve fetch all WordPress Blog categories in JSON REST API","datePublished":"2022-03-14T17:13:15+00:00","dateModified":"2022-03-14T17:13:15+00:00","mainEntityOfPage":{"@id":"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/"},"wordCount":138,"publisher":{"@id":"https:\/\/myfreeonlinetools.com\/blog\/#organization"},"articleSection":["MySQL","PHP"],"inLanguage":"en"},{"@type":"WebPage","@id":"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/","url":"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/","name":"With PHP MySQL Query retrieve fetch all WordPress Blog categories in JSON REST API - MyFreeOnlineTools","isPartOf":{"@id":"https:\/\/myfreeonlinetools.com\/blog\/#website"},"datePublished":"2022-03-14T17:13:15+00:00","dateModified":"2022-03-14T17:13:15+00:00","breadcrumb":{"@id":"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/myfreeonlinetools.com\/blog\/with-php-mysql-query-retrieve-fetch-all-wordpress-blog-categories-in-json-rest-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/myfreeonlinetools.com\/blog\/"},{"@type":"ListItem","position":2,"name":"With PHP MySQL Query retrieve fetch all WordPress Blog categories in JSON REST API"}]},{"@type":"WebSite","@id":"https:\/\/myfreeonlinetools.com\/blog\/#website","url":"https:\/\/myfreeonlinetools.com\/blog\/","name":"MyFreeOnlineTools","description":"","publisher":{"@id":"https:\/\/myfreeonlinetools.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/myfreeonlinetools.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/myfreeonlinetools.com\/blog\/#organization","name":"MyFreeOnlineTools","url":"https:\/\/myfreeonlinetools.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/myfreeonlinetools.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/myfreeonlinetools.com\/blog\/wp-content\/uploads\/2019\/11\/myfreeonlinetools-blue.png","contentUrl":"https:\/\/myfreeonlinetools.com\/blog\/wp-content\/uploads\/2019\/11\/myfreeonlinetools-blue.png","width":387,"height":79,"caption":"MyFreeOnlineTools"},"image":{"@id":"https:\/\/myfreeonlinetools.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/myfreeonlinetools.com\/blog\/#\/schema\/person\/b1eb72e57c554e3b33cfeec477efcc3c","name":"myfreeonlinetools","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/myfreeonlinetools.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b870b17c6c7e3b75d7fe0b8bebfc9cf5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b870b17c6c7e3b75d7fe0b8bebfc9cf5?s=96&d=mm&r=g","caption":"myfreeonlinetools"},"description":"Live in Delhi, Working in Gurgaon as Web Designer and Graphic Designer. Developed and Design myfreeonlinetools for online free tools. Also having youtube channel Name with LearnSchoolOnline. Traveling, watching movies, coding are the hobbies.","sameAs":["https:\/\/myfreeonlinetools.com\/","https:\/\/www.facebook.com\/LearnSchoolOnline\/","https:\/\/www.youtube.com\/learnschoolonline"]}]}},"_links":{"self":[{"href":"https:\/\/myfreeonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/1513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myfreeonlinetools.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myfreeonlinetools.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myfreeonlinetools.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myfreeonlinetools.com\/blog\/wp-json\/wp\/v2\/comments?post=1513"}],"version-history":[{"count":0,"href":"https:\/\/myfreeonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/1513\/revisions"}],"wp:attachment":[{"href":"https:\/\/myfreeonlinetools.com\/blog\/wp-json\/wp\/v2\/media?parent=1513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myfreeonlinetools.com\/blog\/wp-json\/wp\/v2\/categories?post=1513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myfreeonlinetools.com\/blog\/wp-json\/wp\/v2\/tags?post=1513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}