I am working on a PHP/WordPress-based website and would like to add 3 navigation links (one previous link and 2 next links) below each post of a custom post type. The links should look like this:
[prev] [next] [next]
My current code is below, but it is not working at the moment. How can I get this code to display the desired links?
$currentID = get_the_ID();
$args = array (
'post_type' => array( 'guides' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'ignore_sticky_posts' =>true,
'post__not_in' => array($currentID)
);
$guides_query = new WP_Query( $args );
if ( $guides_query->have_posts() ) {
echo '<div class="grid-container nxt-article">';
echo '<div class="nxt-article-heading"><h2>Next article</h2></div>';
while ( $guides_query->have_posts() ) {
$guides_query->the_post();
echo '<div class="nxt-article-box"><div class="nxt-article-box-shadow">';
echo '<a class="nxt-article-link" href="'.get_the_permalink().'"></a>';
$gear_category = get_field('gear_category', $products->ID);
if ($gear_category) :
echo '<div class="g_pre_title" style="color:#fff;position:absolute;left:30px;top:25px;">'.$gear_category[0].'</div>';
endif;
echo '<div>';
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'post_grid_image' );
}
echo '</div><div style="padding: 0px 30px 30px 30px;"><div class="nxt-article-box-title">'.get_the_title().'</div>';
$intro_section = get_field('introduction_section', $post->ID);
if( $intro_section ) :
echo '<div class="pg_excerpt" style="height:85px;overflow:hidden;">'.wp_trim_words( $intro_section['wysiwyg'], 32, '...' ).'</div>';
endif;
echo '</div></div></div>';
}
echo '<div style="text-align:center;"><a class="ml_button_style" href="">Show ALL GUIDES</a></div>';
echo '</div>';
} else {
}
wp_reset_postdata();
Related
I made a page template where i can filter some posts. If u do a normal WP_Query everything is fine. But if i use the tax_query it displays no posts.
for registering the post types and taxonomies i use the plugin 'cptui'.
The regular WP_Query request:
$query = new WP_Query(array(
'post_type' => $post->post_name,
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
));
if(!$_POST) { //If not filtered ... then show all
if($query->have_posts()): while ($query->have_posts()) : $query->the_post();
echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
echo "<h5>" . $query->post->post_title . "</h5>";
echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
echo "</div></a>";
endwhile; endif;
}
I read other posts and they said i could use the php echo $GLOBALS['query']->request; to see what the mysql query is.
My query is:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1 = 1
AND wp_posts.post_type = 'vacature'
AND ((wp_posts.post_status = 'publish'))
ORDER BY wp_posts.post_date DESC
LIMIT 6, 6
and now the request with the tax_query.
The tax_query WP_Query request:
$query = new WP_Query(array(
'post_type' => $post->post_name,
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'provincie',
'field' => 'slug',
'terms' => 'noord-brabant',
'include_children' => 0
),
),
));
if(!$_POST) { //If not filtered ... then show all
if($query->have_posts()): while ($query->have_posts()) : $query->the_post();
echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
echo "<h5>" . $query->post->post_title . "</h5>";
echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
echo "</div></a>";
endwhile; endif;
}
The query I get now is:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE 1 = 1
AND (wp_term_relationships.term_taxonomy_id IN (3))
AND wp_posts.post_type = 'vacature'
AND ((wp_posts.post_status = 'publish'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 6, 6
and I get no posts back.
Do you know any solution for this?
Full code of the template:
<div id="page-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_content(); ?>
<?php
global $post;
$paged = ( get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query(array(
'post_type' => $post->post_name,
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'provincie',
'field' => 'slug',
'terms' => array( 'noord-brabant')
)
)
));
// echo $GLOBALS['query']->request;
if(!$_POST) { //If not filtered ... then show all
if($query->have_posts()): while ($query->have_posts()) : $query->the_post();
echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
echo "<h5>" . $query->post->post_title . "</h5>";
echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
echo "</div></a>";
endwhile; endif;
}
else { //now its filtered
$null = true;
if($query->have_posts()): while ($query->have_posts()) : $query->the_post();
$filterconditions = []; //hier komen alle filtercondities in
$keys = array_keys($_POST);
foreach($keys as $key) {
if($_POST[$key] == "on") {
$filter = explode("_",$key);
$field = $filter[0];
$filter = $filter[1];
$taxonomy = get_field( $field );
$term = get_term_by('term_id', $taxonomy, $field);
array_push($filterconditions, '"' . $filter . '"' . '==' . '"' . $term->name . '"');
}
};
$filterconditions = implode('&&', $filterconditions);
// echo $filterconditions;
if(eval("return $filterconditions;")) {
$null = false;
echo "<a href='".get_post_permalink()."'><div style='box-shadow: 0 0 15px 0 rgba(0,0,0,.05); padding: 50px; width: 100%; margin: 5px; '>";
echo "<h5>" . $query->post->post_title . "</h5>";
echo "<p>" . get_field( "plaats" ) . '-' . get_term(get_field('dienstverbanden'))->name . "</p>";
echo "</div></a>";
}
endwhile; endif;
if($null) {
echo "Sorry, er zijn geen vacatures gevonden met deze filters";
}
}
$total_pages = $query->max_num_pages;
if ($total_pages > 1 && !$null) {
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
wp_reset_query();
?>
</div>
$args=array(
'post_type' => 'your_post_type',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'DESC',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'provincie',
'field' => 'slug',
'terms' => array( 'noord-brabant')
)
)
);
$result = new WP_Query( $args );
// The Loop
if ( $result->have_posts() ) {
while ( $result->have_posts() ) {
$result->the_post();
// this is your loop
}
} else {
// nothing
}
Can try above code
You can get simple custom post type list using below code:
$args = array(
'post_type' => 'services',
'post_status' => 'publish',
'posts_per_page' => 6,
'orderby’ => 'title',
'order’ => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print the_title();
the_excerpt();
endwhile;
wp_reset_postdata();
Place your custom post type name to retrieve post and change the while loop according to your need
Ok can you use get post method instead of WP Query:
$posts_array = get_posts(
array(
'posts_per_page' => 6,
'post_type' => $post->post_name, // Get POST TYPE FROM $GLOBAL POST
'tax_query' => array(
array(
'taxonomy' => 'provincie',
'field' => 'slug',
'terms' => 'noord-brabant',
)
)
)
);
trying to put together a wp site for a martial arts centre, and they need a list of their instructors.
i try to get all the children of the instructor page appear in list by their page order, and that works fine, except it only shows the first 10 pages, and there is currently 13 instructor pages...
this is the code i used:
<section id="instruktorer">
<div class="indent">
<?php
$query = new WP_Query( 'pagename=instruktorer' );
$services_id = $query->queried_object->ID;
/* Restore original Post Data */
wp_reset_postdata();
$args = array(
'post_type' => 'page',
'post_parent' => $services_id,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$services_query = new WP_Query( $args );
// The Loop
if ( $services_query->have_posts() ) {
echo '<ul class="instruktorer">';
while ( $services_query->have_posts() ) {
$services_query->the_post();
echo '<li class="clear">';
echo '<a href="' . get_permalink() . '" figure class="instruktorer-thumb">';
the_post_thumbnail('instructor-pic');
echo '</a>';
echo '</figure>';
echo '<div class="caption">' . get_post( get_post_thumbnail_id() )->post_excerpt . '</div>';
echo '</li>';
}
echo '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
</div><!-- .indent -->
</section>
Change your query to this:
$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'post_parent' => $services_id,
'orderby' => 'menu_order',
'order' => 'ASC'
);
The below query works if I manually assign the meta value like so 'meta_value' => '4-5', but I need it to return posts for each of the $cartArr values. How can something like that be done?
<ul id="order-box" class="list-group">';
print_r($cartArr); //Array ( [0] => 3-6 [1] => 4-5 )
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_key ';
}
add_filter('posts_groupby', 'query_group_by_filter');
$the_query = new WP_Query(array(
'post_type' => 'clasa',
'post_status' => 'publish',
'meta_key' => 'twin_id',
'meta_value' => '4-5'
)
);
remove_filter('posts_groupby', 'query_group_by_filter');
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li class="list-group-item" id="remli-'.$post->ID.'">' . get_the_title() . '<a class="right remove" id="remove-'.get_post_meta($post->ID, 'twin_id', true).'" href="#">x</a>';
}
} else {
// no posts found
}
/* Restore original Post Data */
//wp_reset_postdata();
echo '</ul>';
Need to add a foreach loop. like foreach($cartArr as $cartArrr) which is mentioned in bellow line. If working please vote me.
<ul id="order-box" class="list-group">';
<?php
print_r($cartArr); //Array ( [0] => 3-6 [1] => 4-5 )
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_key ';
}
add_filter('posts_groupby', 'query_group_by_filter');
foreach($cartArr as $cartArrr) {
$the_query = new WP_Query(array(
'post_type' => 'clasa',
'post_status' => 'publish',
'meta_key' => 'twin_id',
'meta_value' => $cartArrr
)
);
remove_filter('posts_groupby', 'query_group_by_filter');
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li class="list-group-item" id="remli-'.$post->ID.'">' . get_the_title() . '<a class="right remove" id="remove-'.get_post_meta($post->ID, 'twin_id', true).'" href="#">x</a>';
}
} else {
// no posts found
}
/* Restore original Post Data */
//wp_reset_postdata();
}
echo '</ul>';
I have managed to integrate a custom taxonomies plugin that:
-Filters down the staff depending on what category is selected.
I want it to show that department first, and then the other departments beneath rather than that department all on its own (how it is now).
Here is the link:
http://crippslawtest.co.uk/people/
Here is my Wordpress loop:
<div class="staffwrapper">
<?php
$args = array( 'post_type' => 'cripps_staff', 'posts_per_page' => 300 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="col-md-3 spacetop">';
echo '<a href="'.get_permalink().'">';
echo '<img src="';
echo get_post_meta($post->ID,'image',true);
echo '">';
echo '</a>';
echo '<h2 class="staffname">';
echo get_post_meta($post->ID,'staff_name',true);
echo '</h2>';
echo '<h2 class="staffrole">';
echo get_post_meta($post->ID,'staff_role',true);
echo '</h2>';
echo '<h2 class="staffnumber">';
echo get_post_meta($post->ID,'staff_telephone_number',true);
echo '</h2>';
echo '<h2 class="staffemail">';
echo get_post_meta($post->ID,'staff_email_address',true);
echo '</h2>';
echo '</div>';
endwhile;
?>
</div><!--End of staff wrapper-->
On the results page (search-people.php) I have added a second WP Query using info from this link:
http://codex.wordpress.org/Class_Reference/WP_Query#Methods_and_Properties
This is meant to show all the other posts available. I need the array of $args2 that is: "show all the rest of posts in my staff members custom posts"
Here is my attempt:
//2nd loop
wp_reset_postdata();
$args2 = array(
'post_type' => 'cripps_staff',
'posts_per_page' => '300'
);
$query2 = new WP_Query( $args2 );
while( $query2->have_posts() ) {
$query2->next_post();
echo '<div class="col-md-3 spacetop">';
echo '<a href="'.get_permalink().'">';
echo '<img src="';
echo get_post_meta($post->ID,'image',true);
echo '">';
echo '</a>';
echo '<h2 class="staffname">';
echo get_post_meta($post->ID,'staff_name',true);
echo '</h2>';
echo '<h2 class="staffrole">';
echo get_post_meta($post->ID,'staff_role',true);
echo '</h2>';
echo '<h2 class="staffnumber">';
echo get_post_meta($post->ID,'staff_telephone_number',true);
echo '</h2>';
echo '<h2 class="staffemail">';
echo get_post_meta($post->ID,'staff_email_address',true);
echo '</h2>';
echo '</div>';
wp_reset_postdata();
}
This is creating duplications on my results page, the same post is repeated 6 times! It has taken me ages to get this far, help please :S It should display "all the other posts from Cripps_Staff"
After hours of research, I know it is something to do with adding a double array to my search-people.php that is like:
$args2 = array(
'post_type' => 'cripps_staff',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'Department',
'terms' => array( 'accounting', 'Corporate' )
),
array(
'taxonomy' => 'role',
'field' => 'id',
'terms' => array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ),
'operator' => 'IN'
)
)
);
But I can't for the life of me work out how to organize it properly, any ideas? ANy Wordpress evangelists out there?
I tried this, it is very close!!! What is it retrieving ?
$args2 = array(
'post_type' => 'cripps_staff',
'tax_query' => array(
array(
'taxonomy' => 'Department',
'operator' => 'NOT IN'
)
)
);
I'd make 2 queries, one for the current department and one for the others (excluding the current).
Alternatively, since $loop is an object containing all the query results, you could extract and remove the current department from there.
Of course in the Codex you can find all the details on how to filter queries:
http://codex.wordpress.org/Class_Reference/WP_Query
Also, I suggest you don't repeat get_post_meta and instead use get_post_custom:
https://codex.wordpress.org/Function_Reference/get_post_custom
I'm trying to create a query where I create multiple categories (taxonomies) in a custom post type, and then on the homepage query based on specific which is working fine. Currently I have 3 taxonomies:
current-specials
meineke-difference
featured
I have already written code that pulls these. The problem I'm running into is that on the homepage it needs to only pull these posts when they are also attached to the "featured" taxonomy. So an example of standard logic for this would be:
if taxonomy = current-specials AND featured then success else fail
But what it's doing is pulling them all because the current code is OR, and I need AND
Thoughts? (code below)
<?php
$post_type = 'coupons';
$tax = 'coupons_category';
$tax_terms = get_terms($tax);
if ($tax_terms):
foreach ($tax_terms as $tax_term):
echo '<div id="'.$tax_term->slug.'" class="coupon-box '.$tax_term->slug.'">';
$args = array(
'post_type' => $post_type,
"$tax" => array($tax_term->slug, 'featured'),
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1
);
$myQuery = null;
$myQuery = new WP_Query($args);
if($myQuery->have_posts()):
while ($myQuery->have_posts()) : $myQuery->the_post();
$price = get_field('price_image', $myQuery->ID);
$print = get_field('print', $myQuery->ID);
$product = get_field('product_box_image', $myQuery->ID);
$title = get_the_title();
$content = get_the_content();
echo '<div class="fourty9 left box center">';
echo '<h1>'.$title.'</h1>';
echo '<p class="center"><img src="'.$price.'" /></p>';
echo '<p>'.$content.'</p>';
echo '<p class="center">Print Coupon</p>';
echo '<p class="center"><img src="'.$product.'" alt="Filter"></p>';
echo '</div>';
endwhile;
endif;
echo '</div>';
wp_reset_query();
endforeach;
endif;
?>
You may try this (tax - use taxonomy slug. Deprecated as of Version 3.1 in favor of 'tax_query')
$args = array(
'post_type' => 'coupons',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'coupons_category',
'field' => 'slug',
'terms' => array( 'current-specials', 'featured' ),
'operator' => 'AND'
)
)
);
$query = new WP_Query( $args );