Show two random posts from custom post type

You need an orderby argument. $args = array( ‘post_type’ => ‘custom_advert’, ‘posts_per_page’ => 2, ‘orderby’ => ‘rand’ ); That should pull posts in a random order and stop after retrieving the first two, hence two random posts.

the_content and wp_link_pages

I guess you have: the_content(); wp_link_pages(); in your theme file. So you can instead try the following (PHP 5.4+): /** * Append the wp_link_pages to the content. */ ! is_admin() && add_filter( ‘the_content’, function( $content ) { if( in_the_loop() ) { $args = [ ‘echo’ => false ]; // <– Adjust the arguments to your … Read more

Are there any scenarios where the query_posts may be used?

query_posts() is useful in cases when there is no main query: calls to wp-admin/admin-ajax.php, wp-admin/admin-post.php or wp-login.php for example. Yes, you can achieve the same results there without query_posts() and slightly less compact code instead. But if you don’t have to care about side effects, using query_posts() is acceptable.

Is `query_posts` really slower than secondary query?

You ask: is query_posts really slower than some secondary query… The fact is that if you’re calling query_posts() from a theme then it already is a secondary query. WordPress has already queried the database once to get a certain page then it hits your query_posts() function and queries the database again creating a second query … Read more

Using the loop inside admin

I am not sure about the issue, but my advice would be to try and refactor this to use get_posts() and template tags that can work without $post global variable. Basically do not touch globals at all. Loops in front-end are almost civilized nowadays, but internals of admin are still very wild. 🙂

Adding custom post type to loop

You need something like this in you functions.php file, I’m using the action Pieter suggested. function add_custom_post_type_to_query( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( ‘post_type’, array(‘post’, ‘article’) ); } } add_action( ‘pre_get_posts’, ‘add_custom_post_type_to_query’ ); You can read more about the pre_get_posts in the docs.

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