Custon Content within WordPress Loop

You can use current_post field of WP_Query to check inside the loop, or you can break the loop in two loops. Here are code samples: <?php $services_loop = new WP_Query( array( ‘post_type’ => ‘service’, ‘posts_per_page’ => -1 ) ); while ( $services_loop->have_posts() ) : $services_loop->the_post(); ?> <section class=”single-service”> <div class=”container”> <div class=”row”> <div class=”col-md-6″> <?php … Read more

How to get post ID in a Page?

You should not use globals directly. There are some nice wrapper functions in the WordPress API. If you want to retrieve the ID of the current post or page just use get_the_ID(). Without any test … you could write the functionality in this way: $file_id = get_post_meta( get_the_ID(), ‘sound_s’, true ); $file_url = wp_get_attachment_url( $file_id … Read more

Limit home post to 10 without creating pagination?

If you want to prevent the API from generating pagination links, you can use the found_posts filter to make WordPress think there are never more than 10 posts returned from the current query. add_filter( ‘found_posts’, ‘wpd_disable_home_pagination’, 10, 2 ); function wpd_disable_home_pagination( $found_posts, $query ) { if ( !is_admin() && $query->is_home() && $query->is_main_query() && $found_posts > … Read more

Need Sub-category with main Category

maybe your children are empty, if so, they will not show up by default. you have to set the hide_empty argument to true. plus i cleaned your array a little, first you create one, and than in the next line you add more arguments. keep your code clean.. $optsetlect = (isset($_REQUEST[‘product_cat’]) && !empty($_REQUEST[‘product_cat’])) ? $_REQUEST[‘product_cat’] … Read more

Get all posts which was posted on X Days WordPress

You can try following code $y = date(‘Y’); $m = date(‘m’); $d = date(‘d’,strtotime(“-7 days”)); //Date you want $args = array( ‘posts_per_page’ => -1, ‘post_type’ => ‘post’, ‘date_query’ => array( array( ‘year’ => $y, ‘month’ => $m, ‘day’ => $d, ), ), ); $posts= new WP_Query( $args ); //you will get posts which are posted … Read more

Show post content and title in diferent divs using WP_Query using a loop

PHP loops are loops – you can control them 😉 <?php $the_query = new WP_Query( array(‘posts_per_page’ => 4) ); if ( $the_query->have_posts() ) : $the_query->the_post(); ?> <div class=”grid”> <div class=”main post”> <!–show data of the first post in the array–> <?php the_title(); ?> <?php the_content(); ?> </div> <div class=”nested”> <?php while ( $the_query->have_posts() ) : … Read more

wp_query get data from advanced custom field in wordpress

ACF plugin stores data in the wp_postmeta like the default WordPress custom fields. To get data from a custom field your code should look like this (assuming the custom field name is “brand_name”). $title = get_the_title(); $the_query = new WP_Query( array( ‘posts_per_page’=>9, ‘post_type’=>’product_name’, ‘order’ => ‘ASC’, ‘meta_query’ => array( array( “key” => “brand_name”, “value” => … Read more

Multiple Custom Post Type queries, how to DRY it up

The below example is your code in a loop. The post_types array includes all the post types that you’re querying. Then, the next bit (foreach, more info here) runs the code wrapped in curly braces ({ to }) for each item in that post_types-array. post_types = array(‘recipes’, ‘value_chain’, ‘press’, ‘multimedia’); foreach($post_types as $post_type){ $query = … Read more

Is it possible to query from external database? [duplicate]

You can use wpdb to setup a new connection. Add something similar to this below in your functions.php and you can then query just like you would normally but using the new connection. $second_db = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST); $results = $second_db->get_results($your_query); references: https://wordpress.stackexchange.com/a/1673/86845 https://stackoverflow.com/a/36759606/3204142