I have a PHP-code snippet array showing a list of ACF-field values from posts. I would like to use that code on a single post and only show the ACF-values from the current post. So it´s no longer a list. I still need the date filtering.
What do I have to change for it to work like I intend?
<?php
$today = current_time('Ymd');
$args = array(
'post_type' => 'post',
'posts_per_page' => '20',
'meta_key' => 'kalenderdag',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'kalenderdag',
'compare' => '>=',
'value' => $today,
'type' => 'DATE'
),
),
);
$children = new WP_Query($args);
?>
<?php if ($children->have_posts()) : ?>
<?php while ($children->have_posts()) : $children->the_post(); $fields = (object) get_fields(); ?>
<div class="event row">
<div class="event-logo col-sm-4">
<?php the_post_thumbnail( 'large', array( 'class' => 'img-responsive' ) ); ?>
</div>
<div class="event-details col-sm-8">
<h2 class="underline"><a href="<?php the_permalink(); ?>"><?php echo $fields->kalendertitel; ?> - <?php $date = get_field('kalenderdag'); ?>
<?php echo date("d M Y", strtotime($date)); ?></a></h2>
<h3 class="underline"><?php the_title(); ?></h3>
<p><?php echo $fields->kalendertext; ?></p>
<p class="call-to-action">Läs mer</p>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<h2 class="page-title">Just nu har vi inga bokade evenemang</h2>
<p>
Kika gärna in på denna sida en annan gång eller kontakta oss på info#fredenshus.se om du har några frågor.
</p>
<?php endif; ?>
Can you just put the current ID in as an argument with 'p' => get_the_ID() to your WP_Query?
$args = array(
'p' => get_the_ID(),
'post_type' => 'post',
'posts_per_page' => '20',
'meta_key' => 'kalenderdag',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'kalenderdag',
'compare' => '>=',
'value' => $today,
'type' => 'DATE'
),
),
);
Related
I've a custom loop which should display 3 projects per category in a random order.
The problem is, that the loop always shows all projects in that category.
I've tested the the code on a single page and it works how it should and only showed 3 projects.
So I guess it has something to do with the archive page?!
Here's ist the loop itself:
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
And here's the full code (I guess not that relevant):
<?php
$args = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => 0 );
$categories = get_categories($args);
foreach($categories as $category) {
?>
<div class="row">
<div class="col-lg-4">
<?php
$term_child = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args_child = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => $category->term_id );
?>
<h4><?php echo $category->name; ?></h4>
</div>
<div class="col-lg-8">
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
<?php } ?>
Sorry, it was an extra function which sets the archive posts to 99.
Istead of is_post_type_archive I used is_archive
I am working in my first PHP project and I don't understand what does this piece of code does. May anyone help me please?
I don't work very well with php.
<?php
$args = array(
'post_type' => 'manual-pdf',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
// ['relation'] => 'OR', // use this for a different comparison
array(
'key' => 'wpcf-category',
'value' => 2,
'compare' => '='
),
array(
'key' => 'wpcf-version',
'value' => 1,
'compare' => '='
)
));
query_posts($args);
if ( have_posts() ) :
while (have_posts()): the_post();
?>
<a target="_blank" href="<?=$url = types_render_field("url", array('raw' => 'false'));?>"><? the_title(); ?></a><br><?=$descricao = types_render_field("description", array('raw' => 'false'));?><hr>
<?
endwhile;
else :
endif;
wp_reset_query();
?>
Basically, this part made me confused mostly:
<a target="_blank" href="<?=$url = types_render_field("url", array('raw' => 'false'));?>"><? the_title(); ?></a><br><?=$descricao = types_render_field("description", array('raw' => 'false'));?><hr>
I have a problem showing numbered pagination on only one of my Wordpress pages.
<?php
/**
* Template Name: Sale template
*/
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array(
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => array(
'key' => 'prijsknaller',
'value' => '1',
'compare' => '==',
//'type' => 'date',
),
);
$query = new WP_Query($args);
?>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="product-overview">
<?php if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-md-3">
<div class="featured-block">
<div class="featured-block-image">
<a href="<?php the_permalink(); ?>"><img src="<?php the_field('productafbeelding'); ?>"
alt="<?php the_title(); ?>"></a>
</div>
<div class="featured-block-info">
<h4><?php the_title(); ?></h4>
<?php if (get_field('prijs_oud')) : ?>
<span class="oud"><?php the_field('prijs_oud'); ?></span>
<?php endif; ?>
<span class="nieuw"><?php the_field('prijs_nieuw'); ?></span> p/m<sub>2</sub>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php the_posts_pagination(array(
'prev_text' => '«',
'next_text' => '»',
)); ?>
</div>
</div>
</div>
</div>
</div>
The query returns about 70 products, so I would expect it to show at least 7 numbers. However, it does not show any pagination.
If you give your full code it will be more easier to find out your problem.
May be you use wp_reset_postdata(); before the_posts_pagination();
Please check this.
try this code it surely help you
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array (
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => array (
'key' => 'prijsknaller',
'value' => '1' ,
'compare' => '==',
//'type' => 'date',
),
);
$wp_query = new WP_Query( $args );
Replace your code with this:
<?php
/**
* Template Name: Sale template
*/
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($paged == "1") {
$args = array(
'post_type' => array(
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'offset' => 0,
'meta_query' => array(
'key' => 'prijsknaller',
'value' => '1',
'compare' => '==',
//'type' => 'date',
),
);
} else {
$offset = $paged * 5;
$offset = $offset - 5;
$args = array(
'post_type' => array(
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'offset' => $offset,
'meta_query' => array(
'key' => 'prijsknaller',
'value' => '1',
'compare' => '==',
//'type' => 'date',
),
);
}
$query = new WP_Query($args);
?>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="product-overview">
<?php if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-md-3">
<div class="featured-block">
<div class="featured-block-image">
<a href="<?php the_permalink(); ?>"><img src="<?php the_field('productafbeelding'); ?>"
alt="<?php the_title(); ?>"></a>
</div>
<div class="featured-block-info">
<h4><?php the_title(); ?></h4>
<?php if (get_field('prijs_oud')) : ?>
<span class="oud"><?php the_field('prijs_oud'); ?></span>
<span class="nieuw"><?php the_field('prijs_nieuw'); ?></span> p/m<sub>2</sub>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="pagination-grp">
<?php
$big = 999999999; // need an unlikely integer
//$i=1;
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'prev_text' => __('<'),
'next_text' => __('>'),
'total' => $loop->max_num_pages
));
wp_reset_postdata();
endif;
?>
</div>
</div>
</div>
</div>
</div>
</div>
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$categories = get_categories($args);
// print_r($categories);
foreach ($categories as $cat) { ?>
<section id="<?php echo $cat->slug ?>-section"><?php
$args2 = array(
'post_type' => 'post',
'category_name' => $cat->slug
);
$query = new WP_Query( $args2 );
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();?>
<h2 class="<?php echo $cat->slug ?>-heading">
<?php echo get_the_title() ?>
</h2>
<div class="post-date">
<?php the_time('d. m. Y') ?>
</div>
<div class="post-content">
<?php the_content(); ?>
</div><?php
endwhile;
endif;
wp_reset_postdata(); ?>
</section> <?php
}
gets me all posts from each category inside a section tag with post category as id.
However this also gets me the same result without using new Wp_Query.
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$categories = get_categories($args);
// print_r($categories);
foreach ($categories as $cat) { ?>
<section id="<?php echo $cat->slug ?>-section">
<?php
// Get posts from each category
$args2= array("orderby"=>'name', "category" => $cat->cat_ID);
$posts_in_category = get_posts($args2);
foreach( $posts_in_category as $current_post ) { ?>
<h2 class="<?php echo $cat->slug ?>-heading">
<?php echo $current_post->post_title; ?>
</h2>
<div class="post-date">
<?php the_time('d. m. Y') ?>
<?php echo $current_post->post_date; ?>
<!-- <?php // echo $current_post->the_time('d. m. Y') ?> -->
</div>
<div class="post-content">
<?php the_content(); ?>
<?php echo $current_post->post_content; ?>
</div> <?php
}
?>
</section> <?php
}
In the second approach I cannot set the date as I want to with echo $current_post->the_time('d. m. Y') but would have to format post_date what is kind of tricky.
Not being familiar with not working with new WP_Query but with $args2= array("orderby"=>'name', "category" => $cat->cat_ID); and using get_posts does not feel right to me.
Both approaches query the database for each category, one with new WP_Query and the other with get_posts.
What is the best practise and performant way to query the database so that all posts inside each category are shown inside a section tag with category as id?
I'm building a hotel finder site and want to filter my search results by custom fields when the user searches a location. So for example when a user types in 'London' it will find hotels in London based on some custom fields they have added. But it only works when the search is example.com/?s= if I search a location like example.com/?s=London my pagination doesn't show. But if I add no location and leave the search blank like example.com/?s= pagination shows and works correctly. Would anyone know why? I'm not that great at php so I really need some help. My current search.php file looks like this.
<?php $unique_id = esc_attr( uniqid( 'search-form-' ) ); ?>
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="search" id="<?php echo $unique_id; ?>" class="search-field" value="<?php echo get_search_query(); ?>" name="s" />
<button type="submit" class="search-submit">Search</button>
</form>
<?php $searchm = get_search_query(); ?>
<?php if ( get_search_query() == '' ): ?>
<?php
$args_a = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'CurrentLocation',
'value' => array($areauserwants, areauserwants2),
'compare' => 'IN',
),
array(
'key' => 'AreaLookingFor',
'value' => $areaihave,
'type' => '',
'compare' => '=',
),
array(
'key' => 'NumberOfBeds',
'value' => $howmanybedsdoiwant,
'type' => '',
'compare' => 'IN',
),
array(
'key' => 'TypeOfHotel',
'value' => $hoteltype,
'type' => '',
'compare' => 'IN',
),
),
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
); ?>
<?php $query_a = new WP_Query( $args_a ); ?>
<?php else : ?>
<?php
$args_a = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'CurrentLocation',
'value' => $searchm,
'compare' => '=',
),
array(
'key' => 'AreaLookingFor',
'value' => $areaihave,
'type' => '',
'compare' => '=',
),
),
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
); ?>
<?php $query_a = new WP_Query( $args_a ); ?>
<?php endif; ?>
<?php if ( $query_a->have_posts() ) : ?>
<ul>
<!-- the loop -->
<?php while ( $query_a->have_posts() ) : $query_a->the_post(); ?>
<?php get_template_part( 'template-parts/post/content', 'excerpt' ); ?>
<?php endwhile; ?>
<!-- end of the loop -->
</ul>
<?php next_posts_link( 'Next', $custom_query->max_num_pages );
previous_posts_link( 'Previous' ); ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'No homes matched your criteria' ); ?></p>
<?php endif; ?>