Configure Widget Logic to display related data using PHP - php

I have installed Widget Logic on a Wordpress site. Using is_single('this') it correctly displays the widget on the page. But, I need
'this' to be a variable and match another variable.
What I'm looking for is the PHP equivalent of a SQL "where" clause that will match one field to another. Select x from y where a=b;
I have posts that represent a "Location" - could be London, Paris, Munich.
I have posts that represent "Services" and can be filtered by location.
I have a widget that displays available services.
I want to dynamically filter what is displayed by the widget according to the location. If I have a Munich post, only those services available in Munich are displayed in the widget and only those services available in London are displayed on the London page.
I've tried setting Widget Logic to about 20 variations of 'field1'=='field2' or
$field1==$field2 and is_??(??) / in_??(??) No joy.
Maybe I have the syntax wrong? or using the wrong WP conditional tag?

Using Widget Logic for this didn't work out. The following runs in a PHP Text box widget on the WordPress page
$fish is the title of the post being displayed (e.g., London)
The related posts all have a 'meta-value' that includes the location (e.g., London)
<?php
$fish = get_queried_object_id();
if ('$fish' != null ) {
query_posts (array ('post_type' => array('accommodation'), 'meta_value' => $fish,
'orderby' => 'asc', 'showposts' => 3) );
if (have_posts()) : while (have_posts()) : the_post();?>
<h4><a href="<?php the_permalink() ?>">
<?php the_post_thumbnail( 'thumbnail'); ?>
<?php the_title(); ?></a></h4>
<?php endwhile;
endif;
} else {
echo "why is this here?";
}
?>

Related

Wordpress Query Posts from specific subcategory of Custom Post Type

Okay guys, this one has me somewhere between ripping my hair out and kicking in my monitor. It seems no matter what I try, no matter how many times I re-write the query, change terms, change syntax...I get nothing.
So I have a website I am working on where we have a custom post type of 'Vinyl' for vinyl records of in an online collection the client wants displayed alphabetically, with a category of 'vinyls' inside the custom post type section. The client then further wanted to separate things down and create a child category within 'vinyls' called 'vinyl_ae' (Vinyls alphabetically sorted by the first letter A through E). Now, I have the issue where I am trying to query any post at all from the vinyl_ae category/sub-category/whatever the hell it is at this point, and NOTHING turns out. The only way I get any results at all is to set an else conditional for the if have_posts() statement. I'll try to include any all data I can here to help sort this mess out.
Custom post type - name : vinyl
main category - name : VINYLs, slug : vinyls, ID : 3
child category - name : Vinyl A-E, slug : vinyl_ae, ID : 4571
Screen Shots of all my category and sub-category layouts
Here is my working Code Currently (take in mind I have stripped it down so much tonight there is not much left and I have tried so many different solutions to the point where I am practically copying and pasting based on results others are having)
<section id="main">
<div class="content-holder no-spacing">
<div class="container">
<div class="content-inner">
<div id="ajax" class="records row">
<?php
$args = array(
'post_type' => 'post' ,
'posts_per_page' => 6,
'cat' => '4571',
'paged' => get_query_var('paged'),
'post_parent' => $parent);
$mv = new WP_Query($args);
if ( $mv->have_posts() ) {
while ( $q->have_posts() ) {
$serial = get_field('serial');
$mv->the_post(); ?>
<div class="serial-num"><?php echo $serial; ?></div>
<?php } ?>
<?php } else { ?>
<em>Things Still Screwy</em>
<?php } ?>
</div>
</div>
</div>
Thank any and all in advance for any help that can be given, I've thrown in the towel here.
There is two things that you should consider in your sample code:
You should set post_type argument to your custom post type vinyl not the post post type.
If the serial is a field of your post you should call the $mv->the_post(); line first and after that call the $serial = get_field('serial'); statement, because before the_post() call, you can't access post's meta data.

Custom Posts not showing in archive page

I currently have a custom post type of Episode with a taxonomy of podcast. When i run the following loop on my archive page i am seeing that I have 149 posts
<?php
$args2 = array(
'posts_per_page' => 1000,
'post_type' => 'episode',
'podcast' => 'my-episodes',
'post_status' => 'publish'
);
$posts = get_posts($args2);
$count = count($posts);
echo $count;
?>
However, when I'm running this variation of the loop on my archive.php file (copied from the wordpress.org page), i am only getting 130 posts
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<div class="entry">
<?php the_content(); ?>
</div>
<p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The loop is saying that my most recent entry was from the 5th, however i have been creating posts on a regular basis over the last few days. Are there any tests I can run to see why the most recent posts are not showing up on the archive page?
Your code appears to be correct. Double check that the posts that are missing are published and not set as a draft/saved. Also check that your custom taxonomy and post types are correct for the query you are running.
After reviewing the dashboard of the site per the recommendation of Tom here is what I was able to find
The site has a plugin called WPML activated. This allows the site to create content in multiple languages.
When the plugin is initially activated, it will ask for the site's native language and apply that to the existing posts and custom posts. This way if you were on an englsih version of the site you would see content ABC while on a spanish version you would see content XYZ.
There is a separate section inside of a custom post that allows you to make said custom post translatable. If this is not checked then your custom post will not display on any version of the site. The reason being (at least in my 5 minutes of research) is that because there is no language specified, it will not load on the language specific versions of the site
I have done two things to fix this:
I have added the ability to make the custom post translatable,
There is a setting that will allow you to display all content that is not translated on any version of the site as opposed to not displaying any content
Thank you everyone for your help.

Remove Title Link for Specific Category in Wordpress

I'm running Wordpress 4.1. I have two blog pages on my site, and though I don't really know php, I've done some tinkering and figured out how to modify the page templates so each page only displays posts for a specific category. That bit of code looks like this:
<?php query_posts('cat=2'); ?>
That works fine. Page A displays posts from category 1, and Page B displays posts from category 2.
What I'd like to do is disable post title links for one specific category. In other words, Page A would display posts from category 1 (with standard clickable title links), and while Page B would display posts from category 2 (with non-clickable title links).
I'm an HTML/CSS guy, so really out of my depth here, but if there's a way to modify the loop to achieve this, I'd love to learn how. Thanks in advance for any help.
Yes, you can do this using the category.php theme file. When this page is hit, it loads a specific category requested and the posts that fall into that category.
Your theme and loop may look something like this:
<?php single_cat_title(); ?>
<?php echo category_description(); ?>
if (have_posts()) : while (have_posts()) : the_post();
/// display posts from specific category
endwhile; endif;
Or if you don't want to use that page which is designed for that, you can create your own loop:
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => 50 ) );
All together like this:
<?php
/* retrieve unlimited # of posts with an category slug of music */
query_posts( array ( 'category_name' => 'music', 'posts_per_page' => -1 ) );
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
while (have_posts()) : the_post();
the_content( 'Read the full post ยป' );
endwhile;
?>

Wordpress custom field only displaying 10 posts

I've used wordpress Advanced Custom Fields plugin to make my portfolio page. I've setup fields for the image, title, category and a yes/no for weather the project is featured.
My code for the project page is as follows:
<?php
$args = array(
'post_type' => 'project'
);
$query = new WP_Query( $args );
?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/html-header', 'parts/shared/header' ) ); ?>
<h1 class="lead"><?php the_title(); ?></h1>
<div id="triggers">
<strong>Filter:</strong>
<span id="filterDouble">Filter All</span>
<span id="filter1">Filter 1</span>
<span id="filter2">Filter 2</span>
</div>
<ul id="gallery" class="group">
<?php if ( have_posts() ) while ( $query->have_posts() ) : $query->the_post(); ?>
<li class="gallery_image" data-id="id-" data-type="<?php the_field('project_category')?>">
<a rel="prettyPhoto[gallery]" class="zoom" href="<?php echo the_field('image') ?>">
<img class="mag" src="<?php bloginfo('template_url'); ?>/imgs/mag.png"/><div class="thumb_bg"></div>
<?php the_post_thumbnail('portfolio'); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
Everything works fine, i can add projects. However My problem starts when i hit 10 projects. After 10, it doesn't display anymore. I am using jpages (jquery plugin) and filtrify to add filters to filer out the categories. They work fine, i can filter by category and i see the correct images. Even with the plugin scripts removed, i still only see a max of 10 posts. Adding more than 10 simply pushes the earlier images off and it displays the 10 latest.
So how can i stop it from being just 10.. My jpage script sets the pagination to 12 per page, but this doesn't even get a chance to kick in. I am thinking it's a post problem as I'm certain it's not the scripts.
I think I've traced an issue - I tested this: 'posts_per_page' => '20' - Which DOES display my missing posts, however i don't want to set a number as i might need lots. How can i define an unlimited number. Providing this is the issue..
If any other code is needed, please let me know, but i think this is the main part of what's controlling my posts appearing on the page.
You're on the right track.Set the value to -1 to get your desired results.
Link to the documentation: http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
posts_per_page (int) - number of post to show per page (available with Version 2.1, replaced showposts parameter). Use 'posts_per_page'=>-1 to show all posts. Set the 'paged' parameter if pagination is off after using this parameter.

How do I call information from a sub page of a specific parent page in wordpress?

Ladies & Gentlemen, boys & Girls, Guru's, Geeks and Geniuses of all ages.
I am having a problem on my wordpress theme.
I am creating a website for a company which has an online shop. The hierarchy of the shop pages is as follows:
Shop
-Catagory Page
--Product Page
One of the category pages is called "Designs" which has a page ID of 22. What I would like to do in the sidebar <aside> of wordpress is display, as a link, the title and an image from the newest sub-page of the 'design' page. Each product page has an image defined using a custom field called "Product_Image".
I have found this code on stackoverflow which I've modified to display the data I need, but instead of just calling the latest sub-page of the design page it calls the latest page from the whole site.
<?php
$args=array(
'showposts'=>1,
'post_type' => 'page',
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
<?php echo "<img src='" . get_post_meta($post->ID, "Product_Image", true) . "' />"; ?>
</a>
</div>
<?php
endwhile;
}
?>
I am completely new to PHP. I have only just started learning using "Tutsplus - PHP Essentials".
Could anyone help me modify this code so it only displays the information from the latest sub-page of the Design Page.
You can add a new argument to your query to only get pages that are a subpage of the design page. So, modify your $args array to look like this:
$args=array(
'showposts'=>1,
'post_type' => 'page',
'caller_get_posts'=>1,
'post_parent' => '22' // get pages that are children of the design page
);
Leave the rest of your code as is, and it should work.
For future reference, see here for all of the WP_Query parameters: http://codex.wordpress.org/Function_Reference/WP_Query#Parameters

Categories