Use query_posts() twice and show results - php

I have a page template for custom post type Activity. Activities are connected to Tours.
In the page template, I want to show Tours with viewed activity, and tours with suggested viewed activity.
Example:
Tours with Biking:
#1 #2
Tours with Biking suggested:
#5, #6
My code:
<?php
$query_string = 'paged='.$paged;
$query_string.= '&orderby=post_date&order=DESC&post_type=tour&meta_query[tour_activities][key]=tour_activities&meta_query[tour_activities][value]='.$current_page_id.'&meta_query[tour_activities][compare]=LIKE&suppress_filters=0&post_status=publish';
parse_str($query_string, $args);
query_posts($args);
?>
<h4 style="margin-bottom: 20px;">Tours with <?php echo(get_the_title()); ?> suggested</h4>
<?php
$query_string2 = 'paged='.$paged;
$query_string2.= '&orderby=post_date&order=DESC&post_type=tour&meta_query[tour_activities_suggested][key]=tour_activities_suggested&meta_query[tour_activities_suggested][value]='.$current_page_id.'&meta_query[tour_activities_suggested][compare]=LIKE&suppress_filters=0&post_status=publish';
parse_str($query_string2, $args);
query_posts($args);
?>
Problem with my code:
It only shows the second query $query_string2 , nothing shows for $query_string
How do I use query_posts() twice and show results seperately with the same logic of formatting the output in the HTML?
I have custom cards in the page template created by looping through have_posts() and then displaying queried Tours

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.

Wordpress query users and display as taxonomy

I have returned a list of registered users. I am looking to be able to query these results and also generate a click through page. Is that possible.
Im trying to essentially get a list of users by searching (first name for example), then click through to show information about them, on the front end. Its mainly the click through im having difficulty with.
For example if i could click a member and it would go through to a page that had the user first name, last name etc.
<?php
$blogusers = get_users();
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
if ( in_array( 'team_member', (array) $user->roles ) ) {
?>
<div class="member_user">
<a href="<?php echo the_permalink();?>">
<?php
echo $user->first_name." ".$user->last_name;
?>
</a>
</div>
<?php
}
}
?>
If i understood correctly, you already have a list of users, and you want each one of them to be linkable to its own page, where you would display the info you want, such as first name, last name etc.
Firstly you have to query for your users and loop through them to display them in a list. You already have this in your code, but if you want something more advanced a query would look like this:
$authors = get_users(array(
'role__not_in' => array('Subscriber', 'Administrator'),
//if you do not want to list the admins and subscribers
'orderby' => array(
'post_count' => 'DESC',
//order users by post count (how many posts they have)
)
));
foreach ($authors as &$author){
//loop through the users
$user_info = get_userdata($author->ID);
$user_link = get_author_posts_url($author->ID);
?>
<a href="<?php echo $user_link; ?>" style="display:block;">
<?php echo $user_info->first_name.' '.$user_info->last_name; ?>
</a>
<?php
}
Then you need to create the page for the user. WordPress handles this by author.php page. If your theme does not have one, simple create a php file called author.php and place it at the root folder of your theme.
You may edit author.php to include the data you want. However if you remove the list of posts that are displayed by default for the selected user you will loose this functionality in your theme.
So inside author.php you may get the user like so:
//get current user (object):
$current_author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
//get User data by user ID
$user_info = get_userdata($current_author->ID);
//echo first & last name:
echo $user_info->first_name.' '.$user_info->last_name;
If you make a print_r($user_info) you may see what info you can display for the selected user.
You may also use the 'get_the_author_meta' function to get more info from the user's profile, eg
get_the_author_meta('description', $current_author->ID);
You can find more info about this function here: https://developer.wordpress.org/reference/functions/get_the_author_meta/
I would generate a link like this:
<?php
echo $user->first_name." ".$user->last_name;
echo 'view user';
?>
And then query the user ID on the next page to get the desired information.
On the next page you can:
<?php
$user_ID = $_GET['user_id'];
get_header();
?>
and then query this to get pretty much any content related to that user
Two and a half approaches:
You could customize your theme's author archive template to display the desired information. Three author archives are available in the template hierarchy:
author-{nicename}.php
author-{id}.php
author.php
You'd be able to link to https://{site_url}/author/{username} for each user.
However, this may not be ideal if you want to keep that normal author blog archive and add the new profile pages. So what may be better long term is to setup a custom post type for the member profiles, then every time a new "member" user is created run a function that creates a new post representing the user.
You can register that custom post type manually with register_post_type() or if you need help a good plugin is Custom Post Type UI.
A good action for your function to hook into is user_register since it happens immediately after user registration but will have access to user data. Within your function you can create the new post using wp_insert_post().
You can then display the information of that custom post type using the archive-$posttype.php and single-$posttype.php templates.
2 1/2. You may also consider using only the custom post type and not querying for users at all. If this member profile is the only reason some folks are users it may even be more convenient this way. Then you can use the archive-$posttype.php and single-$posttype.php templates easily to display the information you choose.
There is a function for that. Use get_author_posts_url()
In the required template use the following code:
// only return required user based on role
$args = array(
'role' => 'team_member'
);
$blogusers = get_users( $args );
// loop though and create output
foreach ( $blogusers as $user ) : ?>
<div class="member_user">
<a href="<?php echo get_author_posts_url( $user->ID );?>"> // links to author page
<?php echo $user->first_name." ".$user->last_name; ?>
</a>
</div>
<?php endforeach;
You will then need to customise the author.php template in your theme to render as required.

Show a single post on other template/page

Working on a site, and I need to have a separated template/page for showing just that one post.
On the home page (index) Ill loop through my categories and showing just a part of them - events - news - references.
Like this:
<?php
$query = new WP_Query( array( 'category_name' => 'Events' ) );
if ($query->have_posts())
{
while ($query->have_posts()) : $query->the_post();
$haveEvents = true;
if ($eventCount < 3) {
$eventCount++;
?>
<div class="event-tile">
<div class="event-tile-content">
<h3><?php the_title(); ?></h3>
<h4><?php the_time('F jS, Y'); ?></h4>
Lees meer
</div>
</div>
<?php
}
endwhile;
}
Every section has a read all and read specific item button:
I already found out how to display all posts within a category on a separated page named: page-posts.php
On page-posts.php I simply check on which page I am and depending on which page, make a query to show the posts within a specified category
$query;
if (is_page('events')) {
$query = new WP_Query( array( 'category_name' => 'Events' ) );
}
elseif (is_page('news')) {
$query = new WP_Query( array( 'category_name' => 'News' ) );
}
if ($query->have_posts())
{
etc..
Now what I cant figure out (not even after spending like hours on google), how to show a single post from the home page (index.php), on a single-post page...
On the action buttons I use:
Lees meer
Which brings me back to the homescreen. (just started on Wordpress)
In a Wordpress theme single.php is used to display a post. If it does not exist it drops down to using your index.php
Try moving your page-posts.php to single.php
Maybe its better to use archive-events.php. You can make an archive for custom posts.
With that you can do: where events is your custom post-type. Then you land on the events archive page.
As far as i can see your problem is that the post is the global post (from your home). So maybe you can setup_postdata($post), or use the home_url('url') functions.

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

Advanced Custom Fields after the main content

I am using a ACF select field to enable the page admin to select a category of posts to display under the page content. For example, if he selects "Televisions" in the ACF select field, all posts in that category will display after the page content. Here is the code for that bottom part of the page (after the main content), from the page template:
<h2>Learn more about <?php the_field('main_page'); ?></h2>
<ul>
<?php
$main_page=get_field('main_page');
$related_systems = new WP_Query( 'category_name='.$main_page );
while( $related_systems->have_posts() ) : $related_systems->the_post();
if($post->post_type == 'post'):
?>
<li><?php the_title();?></li>
<?php
endif;
endwhile; ?>
</ul>
Here is the ACF settings screenshot
The select field shows up fine on the admin side in all four pages that have the Main four page template, but both get_field('main_page') or the_field('main_page') end up blank (I tested get_field with echo and nothing shows up). How To get the field value in the page template?
I'm using WordPress 3.8.1 and ACF Version 4.3.5
Inside the main content, add $page_id = get_the_ID();.
And in the custom loop, call the fields:
the_field( 'main_page', $page_id );
get_field( 'main_page', $page_id );
Those functions work without an ID if used inside the loop, otherwise we need to specify what post we are requesting.
Also, you can filter the post type when calling WP_Query:
WP_Query ( 'post_type=post&category_name=' . $main_page );

Categories