Automated featured product woocommerce - php

This is my first post in this great community. Normally I find all the answers to my small and large problems, but this I'am sad to say is a mystery to me :(
I need to create some kind of automated function that every day, once a day chooses 3 random products and marks them as featured in my Woocommerce store. And the next day removes these 3 and chooses 3 new once.
Sadly due to complicity of the task(for my), I have no code to show/start from.
Only thing I can find is that it has to be defined in some meta information:
$args = array(
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
)
Truely hope someone can help me :)

I think the easiest solution would be to use transients.
Here's a suggestion, but first a couple of caveats.
if no one visits your site the transients don't always clear at the right times, but if that's an issue you can set up a ping service to automatically hit your site every day.
you don't have precise control over when the transient clears... ex: every day at 3pm.
This suggestion requires you to rework how your theme is displaying these 3 featured products. since we aren't actually looking for the _featured meta.
code for functions.php:
function so_35312355_get_daily_featured_products(){
// Get any existing copy of our transient data
if ( false === ( $daily_featured_products = get_transient( 'daily_featured_products' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'rand',
'posts_per_page' => '3',
);
$daily_featured_products = new WP_Query( $args );
set_transient( 'daily_featured_products', $daily_featured_products, 24 * HOUR_IN_SECONDS );
}
return $daily_featured_products;
}
Alternatively, you could run 2 queries.... one to reset all the featured meta keys and a second to update the meta keys for each of your random 3 products.
A more advanced and complete method would be to use cron jobs. Take a look in the Codex for how to schedule an event, but depending on your needs the transient may be enough.

Related

How can I add post to Custom post type from an array?

Problem: I created lists with {CityName} and {Departments of City} two column in Excell. Cities are categories and departments of city are post.
I want to add post to Custom post type {departments} in WordPress
My Question: How can I add post to Custom post type from an array?
I Tried 3 Way
Hard Way
<?php
$departments = array(
array(
"title" => "Adana Kozan Tech Office",
"cityid" => 153 // or city_nicename
),
...
);
foreach($departments as $c) {
$istname = $c['title'];
$cityid = $c['cityid'];
$station_info = array(
'post_title' => wp_strip_all_tags( $istname ),
'post_content' => wp_strip_all_tags( $cityid ),
'post_type' => 'departments',
'post_category' => array($cityid),
'post_status' => 'publish',
);
wp_insert_post($station_info);
}
?>
This was hard way for me. Because little complicated and I saw critical errors. And I couldn't, It didn't worked. So if you could this way, please don't hesitate to say in comments where I made a mistake.
Note: It worked for adding categories (wp_insert_category).
Middle Way
I thought "how the hell we export posts, that way we import". So I added two dummy department with city category. And exported to pc. And I thought of editing that complicated .xml file. But when I was thinking about it, I found a better way.
Easy Way
WP All Import plugin. Yes.
We are preparing in Excell. Then we save it as a .csv extension.
We also open it with notepad ++ and convert it with Encoding> Convert to UTF-8 and save it.
We enter the WP All Import plugin and determine how to drag it from there step by step, category, content, and which part to add. Next next. Finish

Stop WordPress from filtering out draft posts for guests in get_posts

I have a page on my WordPress site which uses a template and in that template, I have used get_posts to fetch results for my CPT. In my CPT, only one post is allowed to have publish status at a time whilst others stay in draft mode. However, I want guest users (i.e. site visitors who don't have access to an account on my site) be able to visit that page and see those posts in their draft state.
I'm using these args to fetch results
$args = array(
'name' => $slug,
'post_type' => 'catalog',
'post_status' => array('draft', 'publish'),
);
$catalogs = get_posts($args);
The code above works fine for signed in users but it returns empty when I sign out. I'm sure that some WordPress filter is responsible for this. I can't figure out how to make it stop.
Additional info: I can use $wpdb to fetch desired results but I'm hoping that this can also be done by some custom filter. I did my research but couldn't find anything useful and WordPress Codex doesn't contain any information regarding this.
Ok, i think i get what you mean. Try the below snippets
/*
* On drafts should be visible.
*/
function show_drafts( $query ) {
$query->set( 'post_status', array( 'publish', 'draft' ) );
}
add_action( 'pre_get_posts', 'show_drafts' );
this should allow both draft and publish to appear visible to everyone when you use get_posts method.

Android application download wordpress content

I have an android application that need to download (via webservice - php ) some books from a wordpress website.
Those books are structured like this : Book Name -> Category 1 -> Subcategory 1 -> text
All information is stored in wp_posts and all realtions are made with wp_postmeta.
I try to begin from "top-to-bottom" starting with "Book Name" like this :
$args = array(
'posts_per_page' => 8,
'orderby' => 'rand',
'post_type' => 'biblie',
'post_status' => 'publish'
);
$show_albums = get_posts( $args ));
and then with wp_query() using post_type, meta_key,meta_value
But any step i take with this method it gets more and more puzzling
So .... is there a way to have all the information using one interogation? I want to interogate wp by book name and somehow have all the information resulting in one arrayor it is to much from wordpress ?
Hope you understand my english !
Thanks
There are plugins that give you a restful api out of the box which could do what you want. A quick search gave me these:
https://wordpress.org/plugins/json-api/
https://github.com/WP-API/WP-API

How to set maximum number of posts allowed for any specific custom post type in Wordpress

I have an image slider on my Wordpress home page, I have created one simple custom post type home_image_slider which only has 2 fields ie. image url & image title.
Now as I only want to show maximum 5 images on my home slider I want a simple solution using which I can limit Admin from adding more than 5 images or you can say 5 post to this custom post type (as I need to show only 5 images on slider it does not make sense to allow admin to add more than 5 posts).
We can check this at the very beginning of /wp-admin/post-new.php:
add_action('load-post-new.php', 'limit_cpt_so_23862828' );
function limit_cpt_so_23862828()
{
global $typenow;
# Not our post type, bail out
if( 'home_image_slider' !== $typenow )
return;
# Grab all our CPT, adjust the status as needed
$total = get_posts( array(
'post_type' => 'home_image_slider',
'numberposts' => -1,
'post_status' => 'publish,future,draft'
));
# Condition match, block new post
if( $total && count( $total ) >= 5 )
wp_die(
'Sorry, maximum number of posts reached',
'Maximum reached',
array(
'response' => 500,
'back_link' => true
)
);
}
Maybe you can allow draft posts and make sure you're only pulling 5 slides in the frontend setting 'numberposts' => 5 in your WP_Query or get_posts.
Actually It doesn't make sense to limit custom post type and normally they don't do it. Whatever If you like to limit slider post to 5, using theme option page might be better approach. You can either try manually or use appropriate theme options frameworks like
Options framework
Official Documentation
ThemeForest Discussion
Option Tree
Redux

wp_query maximum limit without pagination?

My client has a custom post type called 'Clients'. She uses this to record all information about each client she has. She then sometimes leaves these in either draft mode or published. She would like to be able to see all of her clients at a glance and copy and paste all of their details straight from the web page on a regular basis. I have created the following query (note the query is only showing fist and last name at the moment for testing):
<?php
$args = array(
'post_type' => 'clients',
'post_status' => array('draft', 'publish'),
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$fname = get_field('fname');
$sname = get_field('sname');
echo '<p>'.$fname.', '.$sname.'</p>';
endwhile;
?>
However, this returns no results. When I change the 'posts_per_page' to a number, the query works. However, if this number is above 58 no results are returned again. I think somewhere there is a limit to how much can be output in 1 query. I cannot use pagination at the minute as the client is admit she'd want to see all information on one page. Is there another way of querying this without limits? Or a way to adjust the limit?
Thank you in advance
Without being able to debug it directly I'm not sure why you're seeing this issue. Alternatively you can try using the 'nopaging' => true option instead of the 'posts_per_page' option. Does that method behave?
Are you sure that the custom post type name is clients and not client? That is the only thing that I can point, as I the only parameter that is custom by the developer ^^.
The problem was down to my local setup, as soon as it was uploaded to the clients server the error disappeared. Sorry I forgot to close the question

Categories