How do I find the first item in the post array?

You can get first item using this code <?php global $post; $the_post_ID = $post->ID; $n = get_posts(); $i=1; ?> <?php foreach ( $n as $post ) : ?> <nav id=”postNav”> <ul> <li<?php if ($i){ echo ‘ class=”current”‘; $i=0; }?>> <a href=”https://wordpress.stackexchange.com/questions/88596/<?php the_permalink(); ?>” title=”<?php printf( esc_attr__( ‘Permalink to %s’, ‘twentyten`enter code here`’ ), the_title_attribute( ‘echo=0’ … Read more

Lots of SQL queries

Delete post/page revisions and that will reduce queries as well as the overall size of the DB. Run this in phpmyadmin (backup the DB first): DELETE a,b,c FROM wp_posts a LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id) LEFT JOIN wp_postmeta c ON (a.ID = c.post_id) WHERE a.post_type=”revision” Change table prefix above if needed. And … Read more

Sort posts with multiple meta_keys

WP_Query supports multiple values for the orderby argument (see the example from that link on the Codex. I don’t know if query_posts() does as well, but I suspect the pre_get_posts filter does, in which case you can replace query_posts() with that (which is the new “best practice” anyway).

query_posts works while get_posts doesn’t

Every page has a “main query” which is run before the template is loaded. The results of the main query are how WordPress determines what template to load. The standard Loop operates on the data contained in the main query, this is why it seems to just magically “work” without you having to explicitly query … Read more

prevent display duplicate titles on main page

Your code should look like this to exclude duplicate titles <ul> <?php // Initial counter for displayed posts. $counter = 1; // Initial empty array of displayed titles. $titles = []; $portfolio = new WP_Query([ ‘post_status’ => ‘publish’, ‘post_type’ => ‘post’, ‘cat’ => ” . $link1 . ”, // Because we don’t know the number … Read more