Get random custom posts from a custom post type

You need to include setup_postdata($post); in your foreach line. Here’s some great demo code from the codex, adopted to fit your query: <ul> <?php global $post; $tmp_post = $post; $myposts = get_posts( ‘post_type=predic&numberposts=4&orderby=rand’ ); foreach( $myposts as $post ) : setup_postdata($post); ?> <li><a href=”https://wordpress.stackexchange.com/questions/74243/<?php the_permalink(); ?>”><?php the_title(); ?></a></li> <?php endforeach; ?> <?php $post = $tmp_post; … Read more

How to limit get_posts()?

You can use posts_per_page or numberposts. http://codex.wordpress.org/Template_Tags/get_posts#Parameters EDIT: Regarding your edit. You cannot have the posts_per_page different to the LIMIT in the SQL query. get_posts() creates an instance of WP_Query and returns it using the posts_per_page as the LIMIT.

data returned from get_post($postId) have different keys than wp-json/wp/v2/posts/{postId}

You can set your own keys in the JSON response array. It can be anything. Take a look at this simple example: add_action( ‘rest_api_init’, function () { register_rest_route( ‘shubham’, ‘/get_the_post/’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘get_posts_by_rest’ ) ); }); // Callback function function get_posts_by_rest( ){ // Get the post id from the URL and … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)