Wordpress foreach loop in pagination query - wordpress

I'm trying finish a query using something called a foreach loop but I can't for the life of me get it to work. Can anyone help me finish this.
They pagination query at the top is complete, but you can see in the bottom loop, where by if it displays no pagination links then it will not output any html markup. But if pagination does exist, I can't get it to output the pagination links using a foreach loop.
This question is an extension of this. https://stackoverflow.com/questions/8726656/
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
$links = paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => __('← previous downloads','help'),
'next_text' => __('newer downloads →','help'),
'type' => 'array'
));
?>
<?php if (count($links) > 0) : ?>
<div class="archive-navigation">
<?php
// Display links using a foreach loop.
?>
</div>
<?php endif ?>

In php, a foreach is a looping construct that binds the "current" item to a variable name. Its like saying "do this procedure for each of these items". Try the following:
<?php
foreach($links as $link) {
echo $link;
}
?>
Here, $links should be an array of Strings, although the documentation for paginate_links() isn't crystal clear about this. If you wanted you could echo additional markup in the foreach to help customize the links. To target these links using css:
.archive-navigation a {
// styles, e.g.
margin-right: 5px;
}

Related

Exclude children of custom post type from search

I have a custom post type of 'location'. I then have children pages for each of the pages for that cpt. so it looks something like this, "www.example.com/location/location-name/child-page/", each child page is using a post template of "location-product-services.php". So what I am trying to do is exclude from the search results the children of this cpt.
I am trying to do it by checking the meta data to see if it is using that template. I just cant seem to get it working. Any help would be great.
This is what I currently have -
// Exclude local product and services pages from search result.
function location_subpages_exclude_search( $query ) {
if ( is_search() && !is_admin()) {
$query->set('meta_query',
array(
'key' => '_wp_page_template',
'value' => 'location-product-services.php',
'compare' => '!='
)
);
}
}
add_action('pre_get_posts', 'location_subpages_exclude_search');
Thanks in advance.
First, I pretty much exclusively use the Relevanssi plugin any time I want to modify search. But to search programmatically, I think this is what you're after:
$taxonomy = 'location';
$terms = get_terms($taxonomy, array( 'parent' => 0, 'search' => $query ) );
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
<?php endif;?>
Use the function get_terms to search your CPT, the 'search' is your $query (you might consider wrapping the search string with the SQL wildcard '%') and 'parent'=>0 returns only the top level.
I figured it out.
First I got all parent pages of my post type, used get_pages() grab them all.
Looped through each of the parent pages and ran another get_pages() for children of that parent.
function SearchFilter($query) {
if ($query->is_search) {
$args = array(
'hierarchical' => 0,
'post_type' => 'location',
'parent' => 0, //returns all top level pages
'post_per_page' => -1
);
$parents = get_pages($args);
$excludes = array();
foreach($parents as $parent) :
$args = array(
'post_type' => 'location',
'child_of' => $parent->ID,
'post_per_page' => -1
);
$children = get_pages($args);
foreach($children as $child):
array_push($excludes, $child->ID);
endforeach;
endforeach;
$query->set('post__not_in', $excludes);
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

Wordpress search term pagination broken

I'm having an issue with the pagination in search archives. For some reason whenever I try going to the next page the pagination gives me a 404.
It's a default if (have_posts()) : while (have_posts()) : the_post(); loop.
I don't know how, but I think the reason is because I'm getting redirected to a new page that get's generated somehow. Say I search for stoff. Instead of /page/2/?s=stoff it replaces it with ?attachment_id=550&s=stoff.
Here's what I've tried to do (with no different results)
Change the search query (attachment_id is the same here as well)
Change pagination functions (pagenavi, get_next_posts_link, custom function)
Replacing loop content with just a title
removing header, sidebars, footer
resetting post data
Resetting permalinks (pretty)
You can test the pagination live here (dev location)
Any thoughts on this are greatly appreciated :)
EDIT -
Added Pagination code currently used in search.php
I don't think it should be a problem with the pagination, seeing that I've tried so many different ones, and this pagination still works in every other location at the site, but here it is:
// Numeric Page Navi (built into the theme by default)
function bones_page_navi() {
global $wp_query;
$bignum = 999999999;
if ( $wp_query->max_num_pages <= 1 )
return;
echo '<nav class="pagination">';
echo paginate_links( array(
'base' => str_replace( $bignum, '%#%', html_entity_decode( get_pagenum_link($bignum) ) ),
'format' => '',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
) );
echo '</nav>';
} /* end page navi */
The function get's called after the endwhile;

Let users sort posts in Wordpress

I’d like to create a page for filtering posts based on a number of criteria.
I can work with wp_query and deliver posts quite easily, my problem is that I can’t figure out (nor can I find any answers online about this, believe me I looked) how to let users do this.
Take this for example, returns the posts in order of price (custom field meta value) from highest to lowest with 33 posts.
<?php
$featuredPosts = new WP_Query( array(
'posts_per_page' => 33,
'meta_key'=>'Price',
'orderby' => 'meta_value_num',
'order' => DESC
) );
?>
<?php if ( $featuredPosts->have_posts() ) : ?>
<?php while ( $featuredPosts->have_posts() ) : $featuredPosts->the_post(); ?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<h2 class="price-title"><?php the_title(); ?> </h2>
</article> <!-- end div post -->
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
Now, even after reading and googling, I’ll be damned if I can figure out how I’d implement this on the front end for users to filter posts.
I mean, I know you can append to the URLs in Wordpress to alter the order of posts, but in this context I’m totally lost.
I tried this, but it doesn't work.
<?php
$by_price = esc_url(add_query_arg(array(
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => ASC
)));
$by_date = esc_url(add_query_arg(array(
'orderby' => 'date',
'order' => DESC
)));
?>
<ul>
<li>Order by price</li>
<li>Order by date</li>
</ul>
What I’m trying to achieve is actually quite simple as well, let the user choose the category, choose the price range (guessing I’d write something in JQuery to deliver a value into an field), set the number of results they’d like to be returned.
I’ve tried googling everything under the sun I can think of for this, no dice.
Try Simple Custom Post Order plugin.
It is using AJAX and JavaScript, you don’t have to load anything else. You have to just drag and drop the posts.
OK, I update the code to make it clear:
---I do not think meta_key would be auto-pickup---
functions.php
...
$whitList = array(
'price' => array(
'posts_per_page' => 33,
'meta_key'=>'price',
'orderby'=>'meta_value_num',
'order' => ASC
),
'date' => array(
'posts_per_page' => 33,
'orderby'=>'date',
'order' => DESC
)
);
...
Your first loop php:
<?php
gloabl $whitList; //to use the $whitList in your functions.php.
$aryQuery = $whitList[$_REQUEST['orderby']] ? $whitList[$_REQUEST['orderby']] : $whitList['price'];
$featuredPosts = new WP_Query( $aryQuery );
....
....
?>
For your list page:
<ul>
<?php
gloabl $whitList; //to use the $whitList in your functions.php.
foreach( $whitList as $orderby => $aryOrderBySettings){
?>
<li> Order by <?php echo $orderby;?></li>
<?php
}
?>
</ul>
Using $_GET parameters is the way to go here. First of all you'll want to allow your visitors access to these add these variables. The link approach is fine, overall, so we can generate augmented links by using the add_query_arg to tack on extra parameters to the current URL.
<?php
$urla = add_query_arg( 'sort' => 'price', 'asc' => '1' );
$urld = add_query_arg( 'sort' => 'price', 'asc' => '0' );
?>
Sort by price (asc)
Sort by price (desc)
When clicked, the tacked on variables can thus be detected:
<?php
// Get an allowed sort variable and the order
$sort = isset( $_GET['sort'] ) && in_array( $_GET['sort'], array( 'price' ) ) )
? $_GET['sort'] : null;
$order = isset( $_GET['asc'] ) && $_GET['asc'] == '0' ? 'DESC' : 'ASC';
?>
Now you would augment your main query with the data you just retrieved. If you're using the default way of querying posts on a page you should be able to get away with query_posts, although it is not recomended. And, if you're using a custom loop, simply inject the new arguments into it:
<?php
$args = array();
switch ( $sort ):
case 'price':
$args['order'] = $order;
$args['orderby'] = 'meta_value_num';
$args['meta_key'] = 'price';
break;
default:
break;
endswitch;
$defaults = array( 'posts_per_page' => 33 );
$query = new WP_Query( wp_parse_args( $args, $defaults ) );
?>
You can add more variables, by creating more URLs and buttons to press, and more cases in the switch statement to extend the basic example above.
The first piece of code would go wherever you want your buttons to appear. The second piece of code goes before the third one, which goes before outputting the results.

How to implement Wordpress pagination?

I'm trying to implement pagination for my posts. Though I'm a bit stuck on the php function and also how to call it.
The simple method is this I guess:
<?php posts_nav_link(); ?>
But what if I want custom pagination?
Here is my current code:
<?php
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $total > 1 ) {
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
?>
Is this correct and how do I call it? In index.php? Where in the loop? Thanks.
Edit for clarification: How do I implement this code? At the moment I have put it in my functions.php. So how (and where in the loop) do I 'reference' this function so the pagination is displayed.
There are two ways you can implement this code. It looks like right now you are sort of in between the two.
The first way would be to add your pagination code directly into the template that it will be used in somewhere inside the loop (most likely somewhere right before the closing <?php endwhile; ?> tag). If you are using a single.php template, you would put it in there, if not, put it in index.php. The placing of it inside of the loop depends on where you want the pagination to appear on your page.
The second way is to add the pagination code to the functions.php file (which you have done). However, you will need to revise your code a bit for this. You need to wrap the code within a function, and name that function something. I've used your_custom_pagination for an example. Your functions.php file is most likely already wrapped in php tags, so I have removed those.
function your_custom_pagination() {
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $total > 1 ) {
if ( !$current_page = get_query_var('paged') ) {
$current_page = 1;
}
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
}
Then you'll need to go into the template that you are using and place this code <?php your_custom_pagination(); ?> into the same spot I illustrated above to call the pagination function.
I haven't actually tested your code, so assuming it's valid everything should work.

Wordpress - Custom taxonomy page of custom post type listing by terms

I have a taxonomy-taxonomy.php page that needs to look like so:
CUSTOM POST TYPE TITLE (RESOURCES)
Custom Taxonomy 1 (Resource Types)
Resource Type Term 1 (White Papers)
White Paper post 1
White Paper post 2
White Paper post 3
Resource Type Term 2 (Videos)
Videos post 1
Videos post 2
Videos post 3
Tried to make sense of all the new documentation for Wordpress 3.0, but it only made me more confused as it seems to be mixed up with 2.8.
It's not necessary to transform the object to an array, you can perfectly work with the object without too much hassle. What is curious (at least for me), is that you get something like this:
Array
(
[0] => stdClass Object
(
[term_id] => 7
[name] => Magister comunicaciones aplicadas
[slug] => magister-comunicaciones-aplicadas
[term_group] => 0
[term_taxonomy_id] => 7
[taxonomy] => linea-de-estudio
[description] =>
[parent] => 0
[count] => 4
)
[1] => stdClass Object
(
[term_id] => 8
[name] => Engagement marketing
[slug] => engagement-marketing
[term_group] => 0
[term_taxonomy_id] => 8
[taxonomy] => linea-de-estudio
[description] =>
[parent] => 0
[count] => 5
)
)
It's basically, an array of objects, so you've to treat them that way. For example if I want the name of the the first one:
$myterms = get_terms('taxonomy-name', 'orderby=none&hide_empty');
echo $myterms[0]->name;
If you need to iterate through the elements, you still can use foreach();.
foreach ($myterms as $term) { ?>
<li><?php echo $term->name; ?></li> <?php
} ?>
That way you can post the articles from your taxonomy.
For the custom post types, you'll have to create a loop like this:
$args = array(
'post_type' => 'post-type-name',
'taxonomy' => 'term'
//for example
//'resources' => 'videos'
);
// assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);
// starting loop
while ($wp_query->have_posts()) : $wp_query->the_post();
the_title();
blabla....
endwhile;
Then you can create multiple loops each of one for each taxonomy/term :).
If you want to get even more fancy (don't want to repeat yourself a hundred times) you can include the second loop inside the first one and assign variables to the taxonomy (resources ie) and the terms it has (videos) (from your example only the last one). The idea is that you would have a normal (typical) wordpress loop restricted to the custom post-type and each one of the terms.
foreach ($myterms as $term) : ?>
<li><?php echo $term->name; ?></li> <?php
$term_name = $term->slug;
$args = array(
'post_type' => 'post-type-name',
'taxonomy' => "$term_name"
);
// assigning variables to the loop
global $wp_query;
$wp_query = new WP_Query($args);
// starting loop posting only
while ($wp_query->have_posts()) : $wp_query->the_post();
the_title();
blabla....
endwhile;
endforeach; ?>
Obviously you can do the inverse thing too, create the normal loop for a single-template custom type (it's looks like you have only one), and inside includes all the custom terms.
Not very elegant, but that's the best way I can came up with it :P. Hope that someone can understand this, sounds confusing.
Maybe could it be possible with some callback function?.
Hey manon1165 , I actually just accomplished this. Was a huge pain, hopefully my code snippet will help!
I made a custom page template. And did something along the lines of
<?php $categories = get_terms('taxonomy-name', 'orderby=name&hide_empty=0'); $cats = object_to_array($categories); ?>
Now, just print_r($cats) and you will see the array of the categories.
You will need to convert the object to an array, I did so with.
function object_to_array($data)
{
if(is_array($data) || is_object($data))
{
$result = array();
foreach($data as $key => $value)
{
$result[$key] = object_to_array($value);
}
return $result;
}
return $data;
}
I did
<ul id="cat-list">
<?php foreach($cats as $cat) { ?>
<li><?php echo $cat['name']; ?> (<?php echo $cat['count']; ?>)<br><?php echo $cat['description']; ?></li>
<?php } ?>
</ul>
Hope that helps!
This worked fine for me:-
<?php
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
}
>?

Resources