WP_Query: Modify array - php

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

Related

How can I add navigation links below WordPress posts using PHP?

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();

Limit Custom Post Type loop to 1 result

I have a wordpress page in which I need to load a custom post type at the bottom. There are 8 categories of post types, and I have the loop working so that it pulls from the correct category based on the page that loads. The problem is that I want to limit it to just the latest post in that category. Right now the loop is pulling all posts from a category, and I just want 1. I'm not sure how to limit it in the array. I've tried posts, posts_per_page, and num_pages, but none of them seem to work. Does anyone know the value that I'm missing?
$taxonomy=get_field('show_from');
$args = array(
'post_type' => 'project',
'tax_query' => array(
array(
'taxonomy' => 'project-categories',
'field' => 'slug',
'posts_per_page' => 1,
'terms' => $taxonomy
),
)
);
$query = new WP_Query( $args ); ?>
<?php if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
<div class="cpt-feature" style="background: url('<?php echo $backgroundImg[0]; ?>') center center no-repeat; background-size: cover;">
<?php echo get_the_post_thumbnail( $page->ID, 'thumbnail' ); ?>
<?php
echo "<div class='cpt-overlay'>";
echo "<div class='project-information'>";
echo "<h3>";
the_title();
echo "</h3>";
echo "<p>";
echo get_field('intro_blurb');
echo "<p><a href='" . get_permalink() . "'>View Project</a></p>";
echo "</div><!--project-info-->";
echo "</div><!--cpt-overlay-->";
echo "</div><!--cpt-feature-->";
echo "</a>";
}
} else {
}?>
This is a prime example of how proper indenting / formatting code makes a huge difference in your ability to diagnose / troubleshoot issues.
The problem is actually simple: The posts_per_page argument is in the wrong place - it's nested inside the tax_query, but needs to be at the "top level":
$args = array(
'post_type' => 'project',
// moved this argument out of the tax query
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-categories',
'field' => 'slug',
'terms' => $taxonomy
)
)
);
* Most good IDE's, such as PHPStorm, provide easy-to-use tools to auto-format your code for you - I'd strongly recommend finding and using one - I'm a huge fan of PHPStorm.

orderby menu_order show max 10 pages

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'
);

WordPress Custom Taxonomies WP_Query

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 );

WORDPRESS Simple Query

I have a category query, and in my category query I want to get product (only one) by queried category id (or name or whatsoever)
I start query:
<?wpsc_start_category_query(array('category_group'=> get_option('wpsc_default_category'))); ?>
and then try to use get_posts() function to get product:
$args = array(
'post_type' => 'wpsc-product',
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'wpsc_product_category',
'field' => 'id',
'terms' => $aka
)));
$cat1_posts = get_posts($args);
where $aka is:
$aka = '[wpsc_category_id]';
but when I echo $cat1_posts[0]->ID; it only shows my last product ID for every category. what is the problem? echoing only [wpsc_category_id] works perfect.
I tried EVERYTHING for the last few days. I will buy you cookies for help
I've got to idea, that I need foreach or anything like this
You can use the get_terms() function. So something like this (untested)
<?php
//for each category, show latest post
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_terms( 'wpsc_product_category');
foreach($categories as $category) {
$args=array(
'showposts' => 1,
'post_type' => 'wpsc-product',
'wpsc_product_category' => array($category->slug)
);
$posts=get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post); ?>
<p><?php the_title(); ?></p>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>

Categories