Count published sticky posts on Wordpress - wordpress

I would like to get the number of published sticky posts on Wordpress. I can retrieve the number of sticky posts with
$sticky = count(get_option('sticky_posts'));
but it includes the planned posts, I don't want to include them

You can use WP_Query to filter these posts.
$args = [
'post__in' => get_option('sticky_posts'),
'post_status' => 'publish'
];
$posts = new WP_Query($args);
var_dump( $posts->posts );

Related

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

wordpress one-page website with different php pages

I'm trying to build a wordpress website. I'd like to have a one-page design. I have built 5 pages
each one has its own php file. How can I do to concatenate the 5 pages in one single page?
Thank you in advance for your help,
Fabiana
You may not need to use multiple pages at all but implement inbuilt function for wordpress like shortcodes, or use posts and make multiple queries to the post. You can then design your page (1) in sections and make sure to make $loop = new WP_Query( $args ); to enable multiple queries on the same page. These queries would/should only query post not pages.
Section 1 Layout
$args1 = shortcode_atts (array(
'tag' => 'post-type',
'post_type'=> 'post',
'cat' => '', // Category ID
'posts_per_page' => 1,
'bgimageurl' => ''), $atts);
$query1 = new WP_Query( $args2 );
// The Loop
while ( $query1->have_posts() ) {
$query1->the_post();
the_content();
wp_reset_postdata();
Section 2 Layout
$args2 = shortcode_atts (array(
'tag' => 'userchoice',
'post_type'=> 'post',
'cat' => '', // Category ID
'posts_per_page' => 1,
'bgimageurl' => ''), $atts);
$query2 = new WP_Query( $args2 );
You can take a look at this plugin for wordpress which provides a way to create full screen scrolling pages by using fullPage.js plugin.

How to display all posts of specific custom post types, including normal posts?

I want to display all posts of 2 specific custom post type (causes and evenements), but I also want to include all normal posts. Here's my query :
$args = array(
'post_type' => array( 'causes', 'evenements', 'post' ),
'posts_per_page' => -1,
);
$my_query = null;
$my_query = new WP_Query( $args );
Unfortunately, this only returns normal posts.
When I use this query, it shows all posts of causes and evenements custom posts :
$args = array(
'post_type' => array( 'causes', 'evenements' ),
'posts_per_page' => -1,
);
How can I display all posts of causes, evenements and post post types?
I can't use any value for the post_type because I also have other custom post types that I don't want to include in my query.
Thanks for the help!
Better to use this plugin Ultimate "Category Excluder"
It is simple to use and the ui provided is user friendly.

Get posts not in array OR with specific meta_key in wordpress

I have a problem :)
I have a meta_key 'glowna'. this is checkbox. If I check this it means that post should be shown on main page of category. Also I want to show all posts that have more votes than i.ex. 10. I have code like this now:
Main page:
//posts waiting
$array = $wpdb->get_results("select post_id from $table where (user_recc_plus + visitor_recc_plus < 10)");
in $array I have only Ids
Then I have:
$args=array(
'post__not_in' => $array,
'cat'=>$catId,
'order'=>'desc'
);
Also when I want to display waiting posts I use:
$args=array(
'post__in' => $array,
'cat'=>$catId,
'order'=>'desc'
);
It is working very good. How can I add condition that if post is in $array but has meta_key glowna checked it should be shown. For waiting posts I create:
$args=array(
'post__in' => $array,
'cat'=>$catId,
'meta_key'=>'glowna',
'meta_value'=>'glowna',
'meta_compare'=>'NOT LIKE',
'order'=>'desc'
);
and it seems to be working
But what about posts on main page of category? Can you help me?
I made it like this:
Get all post that has not enought votes:
$posty_poczekalnia = $wpdb->get_results("select post_id from $tabela where (user_recc_plus + visitor_recc_plus < 1)");
Get all posts that are checked as to be shown on main
$args = array(
'cat'=>$katId,
'meta_key'=>'glowna',
'meta_value'=>'glowna',
'meta_compare'=>'LIKE'
);
use diff
$poczekalnia = array_diff($poczekalnia, $glowna);
and get this posts
$args=array(
'post__not_in' => $poczekalnia,
'cat'=>$katId,
'order'=>'desc',
'paged'=>$paged
);

Resources