Wordpress search field affecting custom query - wordpress

Just came across an issue with my Wordpress template that I can't figure out. I am using a custom built menu (done simply with a quick query_posts() call), but when searching for certain terms, my query is affected. Not a clue as to why.
Here's my menu code:
<?php $main_cats=explode(",",$options['main_cats']); ?>
<?php $myargs = array('post_type' => 'page', 'post__in'=>$main_cats,'order'=>'ASC'); ?>
<?php query_posts($myargs);
while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; wp_reset_query();?>
This should end up with 4 menu items. However, when searching for a few "job" related items, it seems that the Job Manager plugin steps in (http://pento.net/projects/wordpress-job-manager-plugin/) and I get one menu result stating "This job doesn't exist". However, I don't understand how that plugin could possible affect my query.
Any thoughts?

This code assume that $main_cats has the right data, before testing this, check that the variable is correct:
<?php
$main_cats=explode(",",$options['main_cats']);
$menu_items=get_posts(array('post_type' => 'page', 'post__in'=>$main_cats,'order'=>'ASC'));
foreach($menu_items as $menu_item){ ?>
<li><?php echo $menu_item->post_title; ?></li>
<?php } ?>

Maybe Use native menu function to to create menu's, http://codex.wordpress.org/Function_Reference/wp_nav_menu
Don't use query_posts($myargs), query_posts() is meant for altering the main loop.
Use WP QUERY http://codex.wordpress.org/Class_Reference/WP_Query (or get_posts).

Try wp_reset_query(); before your query as well as after, like you have. I think that the plugin query is happening before your yours. There may be some carry over from that. Reseting before your query might give you a clean slate.

Related

Wordpress - Two loops on author.php?

Normally if I was doing more than one loop on a page, aside from the main wordpress loop, I'd just use wp_query, however when the author.php template is being used I can't see how I could use that, since I'd have to pass some args.
Pulling posts from categories or by date etc is all easy enough with wp_query, even getting posts by author ID can be done, but it needs to be generic, ie get posts from the current authors page.
Now, using the same loop as my category page I can easily generate posts on the author.php, but I need a second loop and I just can't figure out how to do it.
First loop would be pulling one random post and it's featured image, second would be getting the archive of that authors posts.
Any ideas?
Does this give you a list of the author's posts? If so, it should be easy to modify the arguments for WP_Query to get exactly what you want.
See here for all the options: http://codex.wordpress.org/Class_Reference/WP_Query
<?php
$new_loop = new WP_Query( array(
'post_type' => 'post',
'author' => get_the_author_meta('ID')
) );
?>
<?php if ( $new_loop->have_posts() ) : while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; else: ?>
Sorry, nothing was found.
<?php endif; ?>
<?php wp_reset_query(); ?>

How to display list categories in wordpress integration magento

Can help somebody. I spent several hours to find solution but without results
I tried to display the list of categories on homepage wordpress blog thru following code
<?php $category = Mage::registry('wordpress_category') ?>
<?php if ($category): ?>
<?php echo $category->getId() ?>: <?php echo $category->getName() ?>
<?php endif; ?>
But the method
Mage::registry('wordpress_category')
always return null.
I found that, i should probably be using the Fishpig_Wordpress_Block_Category_View. But i dont know where i should put it.
The following code will retrieve the current category when viewing a category page in your blog:
<?php Mage::registry('wordpress_category') ?>
This is not what you need. To view a list of categories, you could create a custom collection using the following:
<?php $categories = Mage::getResourceModel('wordpress/post_category_collection') ?>
A better way would be to use the category widget block:
<block type="wordpress/sidebar_widget_categories" name="wp.categories" template="wordpress/sidebar/widget/categories.phtml" />
You can create this in PHP using the following code:
<?php echo Mage::getSingleton('core/layout')
->createBlock('wordpress/sidebar_widget_categories')
->setTemplate('wordpress/sidebar/widget/categories.phtml')
->toHtml() ?>
The above code uses the default template, however, feel free to use your own custom template.

Views title is blank

I'm using the Drupal views module to create an overview of nodes. In the view i created i configured the Title. But now when the page is rendered the title isn't shown. It is shown in the breadcrumb etc. But not in the grid template, also if i use another template it still doesn't show. Any idea what this can be? I tried looking for it, but my experience with Drupal is very limited.
I checked the drupal_get_title etc. and it is always returning the title, i think something goes wrong in the views module, but i don't know what :s
Kind regards,
Daan
The problem is most likely how you print the page title. If you want it to happen globally, you should print it in the page.tpl.php. Have you inspected the $title variable in the page template? That is what it's usually called.
i idd removed the title from page.tpl.php, but i did this because i thought it should be printed in the views template. When you check views-view-unformatted.tpl.php etc you see this:
<?php if (!empty($title)): ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
<div class="<?php print $classes[$id]; ?>">
<?php print $row; ?>
</div>
<?php endforeach; ?>
so i thought $title would print the title, but it is fixed just by adding it in my page.tpl.php

WordPress Rewind Posts

I have the following code: http://pastebin.com/ng99augD
It basically spits out five posts for the five authors I have specified in the query posts in each chunk of code and then rewinds the posts prior to the next query. This is far from ideal and would like to make the code more efficient without having to repeat all the template code for the posts over and over. I also want them to be in a random order instead of coming out in the order they have been written as I don't want any particular author to be at the top. Thanks.
I'm not sure why the use of rewind_posts() is necessary. You are creating a new query using by your use of query_posts() each time. rewind_posts() rewinds the query for re-use.
If your performance isn't suffering, it may be okay to run 5 queries to get your five authors. To avoid rewriting the same template code each time you have at least two options. Create a php file with the template code in it, and include it each time you need it:
<?php include('author_posts.php');?>
Or, better yet, create a function in your functions.php or in a plugin file that accepts your query results and outputs the template.
<?php $posts = query_posts('posts_per_page=1&author=author1');
if(function_exists('my_authors')){my_authors($posts);}
?>
Then in your functions.php or plugin:
function my_authors($posts){
//template code here, using $posts->ID, $posts->post_content, etc
}
The third option, which cleans up your code and the number of queries would be to use a category to display posts assigned to it here. You are making 5 queries to display five posts. To use one query to display five posts and sort randomly like you want to, edit each of the five posts and assign them to a new category, lets say:'author sampler'. Then in your template file:
<?php $posts = get_posts('category=author sampler'&order_by=rand&order=asc');
foreach($posts as $post):?>
<?php setup_postdata($post);
//your posted template code follows:
?>
<article id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?><?php edit_post_link('Edit', ' (', ')'); ?></h2>
<p class="p-cat">In: <?php the_category('|') ?></p>
<p class="p-author">
<span class="name">
<?php the_author_posts_link(); ?></span> <span class="avatar"><?php echo get_avatar( $email, $size = '32' ); ?>
</span>
</p>
<?php //etc ?>
<?php endforeach;?>

Getting Permalink from Post ID in Wordpress 3

I'm building a small list of recent comments, and would like to make links to the actual posts that the comments were placed on. Unfortunately, there is no comment_permalink or post_permalink that I can find, so I thought maybe there would be a get_permalink() function, but again, none that I could find on http://codex.wordpress.org/Function_Reference/.
From the $post->ID alone, how can I find the permalink for that particular post? Not that it's completely necessary, but here is what I have so far:
<?php $comments = get_comments( array( 'status'=>'approve', 'number'=>5 ) ); ?>
<p class="recently-posted-comments">Recent Comments</p>
<ul>
<?php foreach ($comments as $comment): $parent = get_post($comment->comment_post_ID); ?>
<li><?php print $comment->comment_author; ?>
on <?php print $parent->post_title; ?></li>
<?php endforeach; ?>
</ul>
My intent is to convert the $parent->post_title into a permalink.
I thought maybe there would be a get_permalink() function, but again, none that I could find.
http://codex.wordpress.org/Function_Reference/get_permalink
I'd also recommend using that over get_page_link()
get_permalink() checks the post type and returns the result of the appropriate function;
Pages use get_page_link()
Attachments use get_attachment_link()
Custom post types use get_post_permalink()
The confusion comes as a result of ambiguous function names. I was looking for something that suggested a link for a "post," yet found nothing. Out of curiousity, I came across and tested get_page_link(), only to find that it does exactly what I was looking for.
Unfortunately I assumed that "page" was an exclusive term reserved for pages in wordpress, rather than posts. It appears in this context it is representative of both.

Resources