-
-
Save SamuelHadsall/c17ea4e438ae782cbdb891d6d77e8ddc to your computer and use it in GitHub Desktop.
Working with wpGrapghql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "data": { | |
| "marketByPath": { | |
| "title": null, | |
| "databaseId": 25250, | |
| "link": "https://unitedvanlines-prod.me/movers/oh/columbus/", | |
| "uri": "https://unitedvanlines-prod.me/movers/oh/columbus/", | |
| "markets": { | |
| "marketName": null | |
| }, | |
| "guid": "https://unitedvanlines-prod.me/movers/oh/columbus/" | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| query GetMatketByPath($path: String!) { | |
| marketByPath(path: $path) { | |
| title(format: RENDERED) | |
| databaseId | |
| link | |
| uri | |
| markets { | |
| marketName | |
| } | |
| guid | |
| } | |
| } | |
| { | |
| "path": "/movers/oh/columbus" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_action( 'graphql_register_types', 'uni_custom_nodeBy_path' ) | |
| function uni_custom_nodeBy_path() { | |
| register_graphql_field( 'RootQuery', 'marketByPath', [ | |
| 'type' => 'Market', | |
| 'args' => [ | |
| 'path' => [ | |
| 'type' => 'String', | |
| 'description' => __( 'The path of the Market to retrieve', $plugin_name ), | |
| ], | |
| ], | |
| 'resolve' => function( $source, $args, $context, $info ) { | |
| try { | |
| // Get the path from the arguments | |
| $path = untrailingslashit( $args['path'] ); | |
| // Get the state abbreviation and market name from the path | |
| $path_segments = explode( '/', trim( $path, '/' ) ); | |
| $state_abbreviation = $path_segments[1]; // Assuming the state abbreviation is the first segment | |
| $market_name = end( $path_segments ); | |
| // Query for the custom post type by state abbreviation and market name | |
| $query_args = [ | |
| 'post_type' => 'market', // Change to your custom post type slug | |
| 'meta_query' => [ | |
| 'relation' => 'AND', | |
| [ | |
| 'key' => 'msa_state_abbreviation', | |
| 'value' => strtoupper($state_abbreviation), | |
| 'compare' => 'LIKE' | |
| ], | |
| [ | |
| 'key' => 'msa_region', | |
| 'value' => ucwords($market_name), | |
| 'compare' => 'LIKE' | |
| ] | |
| ], | |
| 'posts_per_page' => 1, | |
| ]; | |
| $custom_post_query = new WP_Query( $query_args ); | |
| // Check if post was found | |
| if ( $custom_post_query->have_posts() ) { | |
| while ( $custom_post_query->have_posts() ) : | |
| $custom_post_query->the_post(); | |
| $market_id = get_the_ID(); | |
| $market_title = get_the_title( $market_id ); | |
| $market_link = get_permalink( $market_id ); | |
| $market_name = get_field( 'market_name', $market_id ); | |
| error_log($market_name); | |
| // Return a properly formatted object or array based on your GraphQL type definition | |
| return [ | |
| 'databaseId' => $market_id, | |
| 'title' => $market_title, | |
| 'link' => $market_link, | |
| 'uri' => $market_link, | |
| 'market_name' => $market_name, | |
| 'guid' => $market_link, | |
| 'markets' => [ | |
| 'marketName' => $market_name, | |
| ], | |
| // Add other necessary fields | |
| ]; | |
| endwhile; | |
| } | |
| } catch ( Exception $e ) { | |
| // Log the error | |
| error_log( 'GraphQL resolver error: ' . $e->getMessage() ); | |
| // Return null or handle the error accordingly | |
| return null; | |
| } | |
| }, | |
| ] ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment