Hot answers tagged

6

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 and overwriting the ...


4

to hook in the <head> you would use wp_head. You can read the documentation here. add_action( 'wp_head', 'meta_example' ); function meta_example() { echo '<meta name="description" content="Meta Description for this Page." />'; } // End meta_example() This assumes that your theme has <?php wp_head(); ?> in the <head>.


4

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.


3

You simply need the is_sticky() conditional check and then act on that. You would probably still need the is_home() and !is_paged() conditional check to only target stikies on the first page of the home page if ( have_posts() ) : while ( have_posts() ) : the_post(); if ( is_sticky() && is_home() ...


3

In general, the query performed on the homepage, query_posts( 'posts_per_page=get_option( 'posts_per_page' ) and $q = new WP_Query( 'posts_per_page=get_option( 'posts_per_page' ) should have the same exact performance with very very little to no difference between them as all of the above are exactly the same by default (that is, have the same query ...


3

Whenever the main query is available, which is for every front end page load regardless of which page/archive is loaded, you should use pre_get_posts to alter the main query's query vars before the SQL query is build and executed. This goes for each and every page where you need to alter the main query. This is the RECOMMENDED way to alter the main query. ...


2

There are many globals that are being set in a loop, not only $post, and you need to set all of them to get exactly the same behavior. To get this it is not enough to set $post but you also need to use setup_postdata


2

1 If you look at the source code of get_the_title you will see that there is no difference between the two ways of writing the same thing, because if you call it without a parameter it is supposed to be the current ID. 2 There is no substantial difference between the two loops you use. All go through the same amount of items and do the same thing with it. ...


2

Now you can simply use the get_the_excerpt( $postID ) function. Since: WordPress 4.5.0 introduced the $post parameter.


2

I was able to figure this out and make it dynamic. I created a variable $template that I put inside the loop, in which I stored the page template. $template = get_post_meta( $post->ID, '_wp_page_template', true ); Then, I utilize this where I need the child pages to show up. <?php include(locate_template($template)); ?> This is working for me ...


1

You'll have to move the part that says $do_not_duplicate = $post->ID; to the conditional statement that leads to the first get_template_part(). Now you have stored the ID of that sticky post. if( is_home() && !$paged && $c == 1) { $do_not_duplicate = $post->ID; get_template_part( 'includes/sticky-post', 'page' ); } Next, ...


1

The question you linked to doesn't quite do what you're wanting - it allows you to get the template assigned to a page you're querying, but what you're actually wanting is to get a page assigned to a particular template. You can do this using the get_pages() function by querying a meta key of the page. Pages/posts can have a range of fields assigned to ...


1

Try this code global $myOffset; $myOffset = 1; $paged = get_query_var('paged') ? get_query_var('paged') : 1; //Use 'page' instead of 'paged' if you are on home page $args = array( 'post_type' => 'post', 'cat'=> 1, //Selecting post category by ID to show 'posts_per_page' => 4, //No. of posts to show 'offset' => $myOffset, ...



Only top voted, non community-wiki answers of a minimum length are eligible