Some doubts about how the loop work (trying debugging it)

<?php if (have_posts()) : while (have_posts()) : the_post(); endwhile; else: _e(‘No blog found.’, ‘polymer’); endif; ?> if(have_post) condition is to check if the page contain post, while(have_posts) condition if there is more than one post display in loop, the_post() is used to display the post if(have_post) condition fails it will go to else loop. Tell … Read more

Advanced Custom Fields – Relationship post foreach query

Use the old method of adding the relationship field -> Post Object <?php while (have_posts()) : the_post(); ?> <div class=”entry-content”> <h1 class=”content-headline”>Package: <?php the_title(); ?></h1> </div> <div class=”package-description” style=”padding:15px 10px;background:white;”> <?php the_content(); ?> </div> <?php $posts = get_field(‘package_items’); if( $posts ): ?> <ul> <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) … Read more

How do you display the number of a post in a query?

Order number of the post in main query would be $wp_query->current_post + 1 (if I remember right it counts from zero, but try and see). 🙂 Note that literally your example is ordered list, which might or might not have been intended via Markdown. In that case you need to adjust your loop for appropriate … Read more

wpdb query with dynamic column name?

This is simply a syntax error by the looks of it. You are using ” to open the string, and ‘ to close it. Use this: $which_column = ‘phone’; $data_check = $wpdb->get_var(“SELECT ” . $which_column . ” FROM wp_shopping_preferences WHERE wp_user_id = ‘$id'”); Fun fact: when using double quotes (“), you can directed insert a … Read more

Re-sort get_posts query results

Reordering the way you want is really pretty trivial. Proof of concept: $p = array(1,2,3); var_dump($p); array_push($p,array_shift($p)); var_dump($p); With the WordPress code: $p = get_posts(‘posts_per_page=3’); array_push($p,array_shift($p)); var_dump($p); To make it more interesting, you can do the same with a filter. Since get_posts() uses wp_query() you can use the post_results hook to reorder the results. function … Read more

Several taxonomy query (like filter)

You would have to add the tax_query parameters depending on if the value is there in the GET parameters. So it would be something like this, $args = array( ‘post_type’ => ‘post’, ‘tax_query’ => array( ‘relation’ => ‘AND’, ) ); $car = $_GET[“car”]; if(!empty($car)){ array_push( $args[‘tax_query’], array( ‘taxonomy’ => ‘pa_my_taxonomy’, ‘field’ => ‘name’, ‘terms’ => … Read more

Wrap/close ‘query’ for a first-letter ordered listing of posts

The problem is that each time you start a new letter, you don’t close the list of the previous one. Inside your check if ($this_letter != $curr_letter), before the echo, you have to add a </ul> in all cases except when $curr_letter is empty (the first letter). if ($this_letter != $curr_letter) { if(” != $curr_letter){ … Read more

shorthand syntax for custom fields

You have a couple of flaws in your code get_post_meta( ‘fap_referral_link’ ) should be get_post_meta( $post->ID, ‘fap_referral_link’, true ). Please see get_post_meta() You should always first check if you actually have posts before you run your foreach loop Never ever use extract(). It uses eval() which stand for EVIL. This has been completely removed from … Read more

Function to limit number of custom post types on homepage – TwentyTen

This should do the trick: add_action( ‘pre_get_posts’, ‘add_tours_post_type_to_query’ ); function add_tours_post_type_to_query( $query ) { if ( is_home() && $query->is_main_query() ){ $query->set( ‘post_type’, array( ‘tours’ ) ); $query->set( ‘posts_per_page’, 1 ); } return $query; } For the future the Codex can be your friend. Exactly this is explained there.