$reviews = get_posts([
'post_type' => 'reviews',
'post_status' => 'publish',
'posts_per_page' => 10,
'order' => 'ASC',
'orderby' => 'title',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'schema_review_rating',
'value' => $rating,
'compare' => '='
),
array(
'key' => 'schema_review_rating',
'value' => $rating . '.5',
'compare' => '='
)
)
]);
The goal of the previous query is to retrieve all posts with a rating of 4 or 4.5, and display them ordered by post title. Any suggestions on how to proceed?
]]>However, I’m facing a very strange issue. If a product has global addons, 1 in 20 times the global addons are not added to the cart.
I found that when this happens, the function get_posts() (in WC product add-ons plugin) returns an empty array when it should return the global addons posts IDs. This only happens on an ajax call randomly (1 in 20 times aprox).
I confirmed that the arguments passed to the functions are the same on both page load and Ajax call, even when the issue occurs.
Any idea why get_posts() is behaving like this?
]]>I want to show max. 2 sticky posts on my blog in a specific loop. But if there is only one or no sticky post, the loop shouldn’t show “normal”posts.
But the following code shows “normal” posts if there are not 2 or more sticky posts:
$args = array(
'numberposts' => 2,
'post__in' => get_option( 'sticky_posts'),
'ignore_sticky_posts' => 1
);
$sticky_posts = get_posts( $args );
Thank you a lot in advance!
Daniel
getnews()
nothing prints.
<?php function getnews() { $args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title' ); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?>
<?php the_title(); ?> <?php the_excerpt(); ?>
For the heredoc attempt I have $newsletter = 'getnews';
and refer to it so: {$newsletter()}
without success.
Thanks for any help sorting this out.
]]><?php echo get_the_category(); ?>
but its not working. Thank you ”
<?php
// Set the arguments for the query
$args = array(
'numberposts' => -1, // -1 is for all
'post_type' => 'custom post type name', // or 'post', 'page'
'orderby' => 'title', // or 'date', 'rand'
'order' => 'ASC', // or 'DESC'
// 'category' => $category_id,
// 'exclude' => get_the_ID()
);
// Get the posts
$myposts = get_posts($args);
// If there are posts
if($myposts):
// Loop the posts
foreach ($myposts as $mypost):
?>
<div class="row">
<!-- Content -->
<div class="col-xs-9">
<a href="<?php echo get_permalink($mypost->ID); ?>"> <?php echo get_the_title($mypost->ID); ?></a>
</div>
</div>
<?php endforeach; wp_reset_postdata(); ?>
<?php endif; ?>
]]>The first hook is for ‘pre_get_posts‘, and the other hook is for a similar function ‘alm_query_args_{id}‘ from the plugin AJAX Load More.
]]>WP query doesn’t seem working as expected with PHP8 when it’s used in an AJAX callback function.
Both with WP_Query and get_posts, using $post->ID or get_the_ID() within the foreach following the query returns NULL.
I tested multiple scenarios, w/wo wp_setup_postdata, $wpdb->get_posts, etc., and the result was always the same.
Thanks for investigating this further…
]]>My situation is the following:
In my WP I’ve created Custom Post Type called car
and Custom Taxonomy called car_user_rating
with CPT UI plugin. For car_rating
I created values like: best_speed
, best_color
, best_stearing
, best_breakes
. I want to manually reorder card withing car_user_rating
.
I set up ReOrder Post settings as I wanted. Everything is fine here in Admin.
Now I want to create a shortcode function to insert sorted lists of cars into my WP posts and pages. I want to insert lists according to the car_user_rating
.
I cannot figure out how I would call my lists to display?
Please help!
p.s. I have checked the code of the plugin, docs, github – didn’t find anything about it. I am absolute newbie in PHP. Sorry for such silly question.
]]>I have single-cpt-1.php, single-cpt-2.php, single-cpt-3.php and single-cpt-4.php and the list of links renders on each. If I’m on single-cpt-2.php I’d like to style that link differently than the others. Is this possible using get_posts?
<?php
// get solution cpt's to create the link bar
$posts = get_posts(array(
'post_type' => 'solution',
'posts_per_page' => -1,
'meta_key' => 'posts_order',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if ($posts) : ?>
<!-- links to solutions -->
<div class="navigation-bar-wrap container--narrow">
<ul role="navigation" aria-label="navigation-bar" id="navigation-bar" class="navigation-bar">
<?php foreach ($posts as $post) :
setup_postdata($post)
?>
<li class="navigation-bar-li">
<a class="btn btn--blue hvr-push navigation-bar-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
]]>