Render WooCommerce Handpicked Products Block from a plugin - woocommerce

In the plugin code I have:
add_action( 'wp_loaded', 'show_widget');
function show_widget(){
$block_name = 'woocommerce/handpicked-products';
$converted_block = new WP_Block_Parser_Block( $block_name, array(
'query' => new WP_Query( array (
'post__in' => $products, // $products is a given array of product IDs
'post_type' => 'product'
) )
), array(), '', array() );
$serialized_block = serialize_block( (array) $converted_block );
echo $serialized_block;
}
as a result, i see a commented out wp:query when I view source:
yet nothing is actually painted on the page. Notice there is one product in the result set and I expected it to be rendered to the screen.
Why doesn't it? What am I missing?

switched serialize_block with render_block and it works...

Related

WordPress XMLRPC - Search Post by Slug and Update

Is there anyway to search posts by slug through XMLRPC
https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPosts
getPosts() doesn't seem to return using "name"..
$args = array(
'name' => 'my-slug',
'number' => 1
);
$post = $wpClient->getPosts( $args );
Please let me know if there is a workaround for this, I need to search by slug and then update those slugs remotely via XMLRPC. cheers
I ended up using Methods, this may help someone and save time.. paste the following code in functions.php of the domain you are fetching data from
add_filter('xmlrpc_methods', 'clx_xmlrpc_methods');
function clx_xmlrpc_methods($methods) {
$methods['getPostBySlug'] = 'clx_getpost';
return $methods;
}
function clx_getpost($args) {
global $wp_xmlrpc_server;
$slug = $args["slug"];
$pargs = array(
'name' => $slug,
'post_type' => 'post',
'numberposts' => 1
);
$my_posts = get_posts($pargs);
if( $my_posts ) :
return $my_posts; //echo $my_posts[0]->ID;
endif;
}
from your XMLRPC code use the following to get POST array from slug
$args = array(
'slug' => 'your-post-slug'
);
$postArray = $wpClient->callCustomMethod( 'getPostBySlug', $args );

Sticky posts not showing when using category filter

I am trying to show a specific category post on my home page and in which 1 post is sticky that will appear first but its not working.
I have noticed when I try to show all posts, then sticky post shows first. When I try to show a specific category then its not showing first.
Here is my code:
$sticky = get_option( 'sticky_posts' );
$args = array( 'posts_per_page' => intval($blogtoShow),
'post_status'=>'publish',
'post_type'=>'post',
'cat' => $cattoShow,
'orderby'=>'date',
'post__in' => $sticky);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) :
while( $the_query->have_posts() ) : $the_query->the_post();```
You're looking for the following:
ignore_sticky_posts (boolean) – ignore post stickiness (available since version 3.1, replaced caller_get_posts parameter). false (default): move sticky posts to the start of the set. true: do not move sticky posts to the start of the set.
If you're using a pre-made theme the default might have been modified.
You should add this to your arguments array: 'ignore_sticky_posts' => 0.
A comma should separate each arguments. (Not tested but should work)
More info on the worpress query: https://developer.wordpress.org/reference/classes/wp_query/
----------
EDIT 1.1: I think you need to display a specific template for the sticky (as it's not considered a normal post). At the begining of your loop can you try the following?
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 3,
'post__in' => $sticky,
);
$query = new WP_Query( $args );
if ( $sticky[0] ) {
// insert sticky template...
} else {
// insert posts template...
}

Get Woocommerce product by slug

I have a function to grab products by their category and return specific data about them using their category slug like so:
$itemArgs = array(
'post_type' => 'product',
'posts_per_page' => 1000,
'product_cat' => $request['id'],
'include_children' => false
);
$loop = new WP_Query( $itemArgs );
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
//DO STUFF
endwhile; endif;
Which works fantastically. What I need now is to get a product by its own slug. Where the one above might get every product for "Cookies" category, this one should return just the "Chocolate Chip" product.
I've tried replacing 'product_cat' with 'slug' and 'product_slug' but those don't appear to work. This seems like a fairly straightforward thing to do... there's documentation on finding a product by 48 different properties... slug is not one for some reason. I just get the entirety of the product collection returned.
If you look at the Wordpress codex post about the WP_Query You will see a part which talks about Page and Posts Parameters. There you will see you can use the name parameter.
Assuming your product slug is chocolate-chip, you can use this to retrieve your product with the post_type and name parameters:
$itemArgs = array(
'post_type' => 'product',
'name' => 'chocolate-chip'
);
$query = new WP_Query($itemArgs);
$product_obj = get_page_by_path( $slug, OBJECT, 'product' );
https://wordpress.stackexchange.com/questions/206886/get-product-details-by-url-key-in-wordpress-woocommerce

Creating a page with wp_insert_post()

I have a form that collects data about a user's product and then creates a page in WordPress with
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
The form data is put into meta variables for the page ID and the listing.php template file pulls it out of there and builds the HTML to display the product page. This all works fine and I can see that the page meta variable, _wp_page_template, gets set correctly to the template file I specified, listing.php:
Now I want to create a second page from the same form data, this one displaying different parts of the data in a different way. So I've added a second block of code, starting at $my_cert below, that creates this second page and specifies a different template, certificate.php, that knows how to build the second version of the data.
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
$my_cert = array(
'post_content' => "My certificate", // post_content is required
'post_title' => "My certificate", // post_title is required
'post_name' => "My certificate",
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "certificate.php",
'post_status' => "publish"
);
$CERT_ID = wp_insert_post( $my_cert );
$cert_permalink = get_permalink($CERT_ID);
echo "<br />ID for new certificate is $CERT_ID, Permalink for new certificate is $cert_permalink";
But when I look in the meta data for the second page created, the template is set to "default" instead of certificate.php:
I know I've set up certificate.php correctly as a template (set /* Template Name: certificate */ at the top) because the Page Edit Template dropdown includes certificate:
So does anyone see why I can't create this second page with the template set to certificate.php?
Thanks
Are you sure your page template src for: certificate.php is: certificate.php? And not: templates/certificate.php or something like that. Look in your theme folder and be 100% of the page template path. Check your spelling or for typos in the page template path or name. It must be an exact match.
If you still have problems I would look into and debug the source code of: wp_insert_post()
if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) {
$post->page_template = $postarr['page_template'];
$page_templates = wp_get_theme()->get_page_templates( $post );
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
if ( $wp_error ) {
return new WP_Error('invalid_page_template', __('The page template is invalid.'));
}
update_post_meta( $post_ID, '_wp_page_template', 'default' );
} else {
update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
}
}
So its probably this part that fails:
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) )
Try to modify: wp-includes/post.php and go to the definition of: function wp_insert_post() on row: 2872. And add a new row on row: 3312 for debugging purposes.
echo '<pre>';
print_r( $page_templates );
echo '</pre>';
die();
Make sure your certificate.php is among those in that array. Remember to delete the debug code before continuing. This should give you some answers.

Does "Advanced Custom Fields" taxonomy field support user pages?

I have a "taxonomy" custom field for the user pages. I want to build a query filtered by this field. It works with normal querys but not with user-querys, am i doing something wrong?
<?php
$args = array(
'key' => 'fruits',
'value' => 'apple'
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo $user->display_name;
}
} else {
echo 'No users found.';
}
?>
Try get_users instead:
$users = get_users(array(
'meta_key' => 'fruits',
'meta_value' => 'apple'
));
var_export($users);
Wordpress codex: get_users()
Edit:
After a bit of research it turns out that get_users() is only a wrapper for WP_user_query, so switching to this function will make no difference.
However... did you notice that in my answer (and vrajesh') we have substituted your key with meta_key, and value with meta_value ... They are definitely defined in the WP_User_Query class, so I would be surprised if they didn't have any meaning.
If by chance you are using your original $args (which I guess does not actually refer to fruits and apples), then that may well be the explanation you are getting nothing.
try this:
$args = array( 'meta_key' => 'fruits', 'meta_value' => 'apple','compare' => '=');
Use WP_Query instead of WP_User_Query. WP_User_Query is used to retrieve data from user and usermeta table. And according to my understanding your are retrieving data from posts and postmeta table.
Class Reference/WP User Query and
Class Reference/WP Query
UPDATED:
Try this
<?php
$args = array(
'meta_query' => array(
'key' => 'fruits',
'value' => 'apple',
'compare' => '='
)
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo $user->display_name;
}
} else {
echo 'No users found.';
}
?>

Resources