Custom Post Meta Search Template - php

I'm struggling with getting wordpress to search custom post meta from a page template. I've looked all over the internet and can't seem to find anything that will work. No plugins seem to work either.
On my posts, I have the custom meta: "rate" and the value: "10" - Wordpress delivers no result when searching any of these.
I'd be very appreciated if someone could write me a searchpage.php page template or point me in the right direction (I'm not good with php).
Here's my current PHP code:
<?php
/*
Template Name: Search Custom Meta
*/
?>
<?php get_header(); ?>
<div id="content">
<div class="container clearfix fullwidth">
<div id="left-area">
<?php query_posts('meta_value='.$s); ?>
<?php if (!empty($wp_query->posts)) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
</div> <!-- end #left-area -->
</div> <!-- .container -->
</div> <!-- #content -->
<?php get_footer(); ?>

You checking incorrect variable in if so try removing that one,
query_posts('meta_value='.$s);
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;

Related

HTML only print once in Wordpress Loop

I have a code chunk that is inside the_content(); I'm using acf repeater as well. So when I post a blog, I'll either use the_content(); or the acf field. I have h2 tag ( latest articles ) that I only want printed one time, but it's printing everytime I make a post.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="container">
<div class="row">
<div class="col-md-4 sidebar-r">
<?php echo the_content(); ?>
</div><!-- end sidebar-r -->
<?php
$i = $wp_query->post_count;
if($i <=1) {
echo '<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>';
}else{
echo '';
}
?>
<div class="col-md-8 links-wrap">
<?php if(have_rows('daily_links')): ?>
<?php while(have_rows('daily_links')): the_row(); ?>
<a href="<?php the_sub_field('link_url'); ?>" target="_blank">
<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>
<h3 class="link-source">
<?php the_sub_field('link_source'); ?>
</h3>
</a>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- end links wrap -->
</div><!-- end row -->
</div><!-- end container -->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
You'll see I tried using php to count the posts and if more than one post, don't print the tag, but couldn't figure out the exact logic and syntax.
I am honestly struggling a bit to understand exactly what you are trying to do and since I do not even have the posts and other key pieces of information so that I can properly replicate your issue so that I can help you better, this is a little bit challenging. That being said, looking into some ideas I came across another stackoverflow question/answer that might be relevant for you in catching the first post and does something to it. The answer to the referenced question instance was this:
<?php if (have_posts()) : $postCount = 1; while (have_posts()) : $postCount++; ?>
<?php if($postCount == 2) { ?>
// SOMETHING TO DO WITH FIRST POST
<?php } else { ?>
// SOMETHING TO DO WITH ALL OTHER POSTS
<?php } ?>
This was suggested by user Bora in this answer from 2013.
Let me know if that helped!

making first wordpress theme, the loop and template functions not working properly

I am following along with this tutorial and what I have made so far is not working as it should. I have gotten to the point where I am starting to use the loop - this is my code:
index.php
<?php get_header(); ?>
<div class="row">
<div class="col-sm-8 blog-main">
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; endif;
?>
</div><!-- /.blog-main -->
<?php get_sidebar(); ?>
</div><!-- /.row -->
<?php get_footer(); ?>
</div><!-- /.container -->`
content.php
<div class="blog-post">
<h2 class="blog-post-title"><?php the_title(); ?></h2>
<p class="blog-post-meta"><?php the_date(); ?><?php the_author(); ?></p>
<?php the_content(); ?>
</div><!-- /.blog-post -->
I have two posts but only the latest post is getting displayed (by the loop) and within the post that gets displayed - the only part that is displayed is the title. So it seems like the_date(), the_author(), and the_content() functions are not working for some reason, and the loop is only getting one of the posts (the latest one).
What is wrong? Thanks.
I'm not sure what I did, but I went to WP admin and clicked "customize" for the theme, and it loaded correctly in the theme editor - and then I went to the site, and it was working. I think maybe I didn't save a file, and I didn't realize it - and as I was trying to figure it out, I ended up saving the file, and it started working.

Customize search results by category

I have different categories where the post displayed are characterized by different elements, for this reason when I use the search in wordpress the results are not shown very well.
For this reason I would like to change the appearance of the post shown in the search results based on its category.
Example:
My search.php template look like this:
<?php get_header(); ?>
<div id="content">
<div class="padder">
<?php do_action( 'bp_before_blog_search' ); ?>
<div class="page" id="blog-search" role="main">
<?php if (have_posts()) : ?>
<?php bp_dtheme_content_nav( 'nav-above' ); ?>
<?php while (have_posts()) : the_post(); ?>
<?php do_action( 'bp_before_blog_post' ); ?>
<div class="blog-post">
// Here is displayed the blog post style and features
</div><!--blog-post-->
<div class="clear"> </div>
<?php do_action( 'bp_after_blog_post' ); ?>
<?php endwhile; ?>
<?php bp_dtheme_content_nav( 'nav-below' ); ?>
<?php else : ?>
<h2 class="center"><?php _e( 'No posts found. Try a different search?', 'OneCommunity' ); ?></h2>
<?php endif; ?>
<div style="display:inline">
<center><?php wp_pagenavi(); ?></center>
</div>
</div>
<?php do_action( 'bp_after_blog_search' ); ?>
</div><!-- .padder -->
</div><!-- #content -->
<div id="sidebar">
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-blog')) : ?><?php endif; ?>
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-ad-blog')) : ?><?php endif; ?>
</div><!--sidebar ends-->
<?php get_footer(); ?>
I tried to achieve the result of displaying based on the post category a different "template" for the post but without success.
I asked the same question on wordpress.stackexchange.com but without success, and there I pointed out a non working solution, hope it can help you in developing the code I need.
https://wordpress.stackexchange.com/questions/141856/different-layout-on-search-page-depending-on-category-post
Thank you very much for any help you will give, and excuse me for my english.
If I understand your question correctly, you could simply use the post_class() function:
<div <?php post_class( 'blog-post' );?> >
// Here is displayed the blog post style and features
</div><!--blog-post-->
This will render like:
<div class="post-3654 post type-post status-publish format-standard hentry category-buildings blog-post">
// Here is displayed the blog post style and features
</div><!--blog-post-->
for posts in the buildings category.
Then your CSS will take care of the rest:
.post.category-buildings {
border: 3px solid red;
}
But I think your setup/idea is only suitable for single category posts.
Ref: From the Codex:
The post_class may include one or more of the following values for the
class attribute, dependent upon the pageview.
.post-id
.post
.attachment
.sticky
.hentry (hAtom microformat pages)
.category-ID
.category-name
.tag-name
.format-name

Serious php get_template_part() Wordpress issues - easy fix?

I am needing some serious Wordpress expertise from others. I am in the process of building a custom theme, read about the get_template_part() method and decided that would help in cleaning up and organizing my code. I updated my theme to use this method several places and now the site is completely broken...not a single thing will load!! :( I would really appreciate any help! I will copy and paste a few of the php files below. Maybe someone with more experience with using this method can point out the problem. THANKS!
page.php
<?php get_header(); ?>
<div id="content" class="content">
<?php while ( have_posts() ) : the_post(); ?>
<!-- About page -->
<?php if (is_page('about')) ?>
<?php get_template_part( 'page', 'about' ); ?>
<!-- Media & Gallery page -->
<?php if (is_page('media-gallery')) ?>
<?php get_template_part( 'page', 'mediagallery' ); ?>
<?php endwhile; // end of the loop. ?>
</div> <!--/end content -->
<?php get_footer(); ?>
page-about.php
<div id="about">
<div class="text">
<h2 class="about"><?php echo get_the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
page-mediagallery.php
<div id="pic-gallery">
<?php include_once 'includes/pwaplusphp/albums.php'; ?>
</div>
index.php
<?php get_header(); ?>
<div class="content">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<br class="clr"/>
<?php endwhile; ?>
<?php content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h3 class="blog"><?php _e( 'Nothing Found' ); ?></h3>
</header> <!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.'); ?></p>
</div> <!-- .entry-content -->
</article> <!-- #post-0 -->
<?php endif; ?>
</div> <!--/end content -->
<?php get_footer(); ?>
content-gallery.php
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-date"><?php the_time(get_option('date_format')); ?></div>
<h3 class="blog"><?php the_title(); ?></h3>
<div class="post-content"><?php the_content(); ?></div>
</div>
Any thoughts why these may not be working? All of these files are under the root directory of the theme so the get_template_part() method should be finding all of the files. What's weird is no code is being spit out by Wordpress. aka. Since I've started using this method, not a single line of code is spit out in my browser when I inspect the source.
I am using the newest version of Wordpress and Google Chrome for my browser.
I also have this code at the top of each of my .php files, but that shouldn't mess up anything because it's commented as seen below:
<?php
/**
* I put a description of what the file does here.
*
* #package Keynote_WP_Themes
* #subpackage Rhymz_Suhreal
* #copyright Rhymz Suhreal, 2012
* #author Kyle Affolder <myemail#example.com>
*/
?>
I don't have the slightest clue as to what I'm doing wrong. :(
Ideas coding friends?!
Using your page-about.php as an example, you should be calling it like this in the template it's to be place in:
<?php get_template_part( "page-about" ); ?>
The template needs to be in the root of your theme directory so you can use "page-about" without the .php, and not have to indicate the location/directory the file would otherwise be located.
I've run into this a few times before as well - I can't really explain why yet as I'm still digging through the documentation, but it seems you can't call template parts this way when you are inside a post loop ;)
Anytime you are inside the loop, try using this instead:
<?php include(get_query_template('page-about')); ?>

How to get custom categories template?

I have a categories page in Wordpress and it has its own template categories.php, which is actually a static page with boxes that link to specific categories. This is a shortened code:
<?php
/*
Template Name: Categories
*/
?>
<?php get_header(); ?>
<nav><?php wp_nav_menu(array('menu' => 'Main Nav Menu')); ?></nav>
<div id="main-content-categories">
<h5 class="inner_text_shadow">Categories & Tags</h5>
<div id="clear-box">
<div id="cc" class="c1"><h6>The Fall Locations</h6></div>
<div id="cc" class="c2"><h6>Info</h6></div>
<div id="cc" class="c3"><h6>Budget</h6></div>
</div> <!-- END main-content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
CSS doesn't matter.
My question is - which PHP file/template is used for those posts that are in certain category like www.mywebsite.com/category/budget? When I click any of the boxes I do get the posts that are in that category but there's no formatting, just the header, sidebar and footer. What is the actual code for "posts from category" and what PHP file do I input it to?
[Same question goes for tags. (www.mywebsite.com/tag/food)]
The category.php can handle all categories. See my example below:
<?php get_header(); ?>
<?php
//Get Category
$category = get_category(get_query_var('cat'));
?>
<!-- Start Loop -->
<?php query_posts('category_name=' . $category->cat_name . '&paged='. get_query_var('paged')); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- Loop Code Here -->
<?php endwhile; else: ?>
<p>An Error Occurred</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
<!-- End Loop -->
<?php get_footer(); ?>
You can also have separate files for each category in the format category-slug.php. In your case you would have category-locations.php, category-info.php & category-budget.php
Hope this helps.

Categories