PHP code example of hsimah-services / wp-graphql-facetwp
1. Go to this page and download the library: Download hsimah-services/wp-graphql-facetwp library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hsimah-services / wp-graphql-facetwp example snippets
// Register facet for Posts
add_action( 'graphql_facetwp_init', function () {
register_graphql_facet_type( 'post' );
} );
// This is the same as all CPTs.
add_action( 'graphql_facetwp_init', function () {
register_graphql_facet_type( 'product' );
});
// This is g['type'];
$use_graphql_pagination = \WPGraphQL\FacetWP\Registry\FacetRegistry::use_graphql_pagination();
return array_merge(
$default_graphql_config,
[
'connectionArgs' => \WPGraphQL\WooCommerce\Connection\Products::get_connection_args(),
'resolveNode' => function ( $node, $_args, $context ) use ( $type ) {
return $context->get_loader( $type )->load_deferred( $node->ID );
},
'resolve' => function ( $source, $args, $context, $info ) use ( $type, $use_graphql_pagination ) {
// If we're using FWP's offset pagination, we need to override the connection args.
if ( ! $use_graphql_pagination ) {
$args['first'] = $source['pager']['per_page'];
}
$resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver( $source, $args, $context, $info, $type );
// Override the connection results with the FWP results.
if( ! empty( $source['results'] ) ) {
$resolver->->set_query_arg( 'post__in', $source['results'] );
}
// Use post__in when delegating sorting to FWP.
if ( ! empty( $source['is_sort'] ) ) {
$resolver->set_query_arg( 'orderby', 'post__in' );
} elseif( 'product' === $type ) {
// If we're relying on WPGQL to sort, we need to to handle WooCommerce meta.
$resolver = Products::set_ordering_query_args( $resolver, $args );
}
return $resolver ->get_connection();
},
]
);
},
100,
2
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.