Using Wordpress, how can I display posts that are in multiple categories? - loops

I am trying to retrieve posts in Wordpress that belong in two categories. Each post must belong in both categories.
Here is my query:
<?php $myPosts = new WP_Query('category_name=featured+news&posts_per_page=10'); ?>
<?php if($myPosts->have_posts()) : ?>
<?php while($myPosts->have_posts()) : $myPosts->the_post(); ?>
<article>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</article>
<?php endwhile; ?>
<?php endif; ?>
In this example, 'featured' and 'news' are the slugs of two categories. I am using the plus symbol (+) with the 'category_name' parameter in my query to indicate that posts must be in both categories, as suggested here.
For some reason, this isn't working and I'm not sure why. I am getting this error:
PHP Warning: urldecode() expects parameter 1 to be string
It's referencing the query.php file:
wp-includes/query.php
Why can the query not interpret featured+news as a string? I figure I must be missing something. Any help is very much appreciated.

I'm not sure why the plus symbol (+) isn't working but using category__and instead works.
$featuredID = get_category_by_slug('featured')->term_id;
$newsID = get_category_by_slug('news')->term_id;
$myPosts = new WP_Query(array('category__and' => array($featuredID, $newsID), 'posts_per_page' => 10));

Related

cakephp Call to member function patchEntities() on boolean

everyone. I have a problem with patchEntities. I want to edit entities by using form.
Edit a entities by using form (view)
Send the data (view)
apply data by using patchEntities (controller)
However, I get an error when I try to patchEntities.
Table Name: tempTable
column: id, name, age
Controller
$data = $this->table->find('all');
if($this->request->is(['post'])){
$entities = $table->patchEntities($data->toArray(), $this->request->data());
}
$this->set(compact('data'));
View
<?= $this->Form->Create('tempTable');?>
<?php foreach ($data as $key=>$d): ?>
<?= $this->Form->Control($key.'.name', ['type' => 'text', 'default' => $d->name]);?>
<?php endforeach; ?>
<?= $this->Form->submit('submit');?>
<?= $this->Form->end();?>
I got an error "Call to member function patchEntities() on boolean"
I can't understand why I get the error.
Could you give me a hint?
Thank you very much.
You didn't execute your query, so you can't use the toArray() method on $data, which is why $data->toArray() returns false (a boolean).
$this->table->find('all') only sets up the query. You'd need to add something like ->all() to execute it.
Try this piece of code instead :
$query = $data = $this->table->find('all');
$data = $query->all();

wordpress loop with pagination only showing two pages

I've created a loop with pagination, to show three posts per page. The pagination is only showing two pages of posts, whereas it should be showing four, as there are 11 posts altogether. The code that I'm using is taken from the WordPress Codex. I'm only just starting out with WordPress development and PHP, so my knowledge is still quite basic. Any help would be appreciated. This is what I have at the moment:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; ?>
<!-- pagination -->
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php else : ?>
<!-- No posts found -->
<?php endif; ?>
Try removing posts_per_page from the query and setting this in the "Blog pages show at most" section in the Reading area in Settings.
Then in your next_post_link(), add the following conditions so it looks like this:
next_posts_link( 'Next Page »', $the_query->max_num_pages );
Note that the first argument can say whatever you want it to say. The second argument is what allows the pagination to work properly. Hope this helps.

CakePHP 3.x How to detect the specific Flash messages?

I have 8 different Flash elements.
4 of them are like these:
growl_success
growl_error
growl_info
growl_warning
The other 4 are:
alert_success
alert_error
alert_info
alert_warning
I want to make it flexible so that at the Controller end, I can freely switch between any of these 8.
The problem is I want the <?= $this->Flash->render() ?> to be at different places in the layout.ctp for growl type messages compared to alert type messages.
Is there a way I can do something like
<?php if ($this->Flash->startsWith('growl') : ?>
<?= $this->Flash->render() ?>
<?php endif; ?>
or
<?= $this->Flash->renderGrowl(); ?>
I think you can use the 'key' options when setting the message
in your controller
$this->Flash->alert_success(__('Alert success!!'), [
'plugin' => 'PluginName', 'key' => 'alert']);
or alternatively
$this->Flash->growl_success(__('The project has been saved.'), [
'plugin' => 'PluginName', 'key' => 'growl']);
in your layout in two different positions
<?= $this->Flash->render('growl'); ?>
...
<?= $this->Flash->render('alert'); ?>

Show calendar to specific users based on custom field

I use the calendar plugin from http://tri.be/ on a worpress website for a private website (it’s like an intranet..).
On this calendar I put all the planned meetings, but I would like that the current logged in user can see only the events he/she is supposed to join.
I thought that I can create a custom field, named, for example, “mustjoin” and put there a list of registered usernames as values.
Then do a check on the page.
If the current logged in username is in that list, he/she can see the event on the calendar.. otherwise not.
Something like :
if ( $current_user->user_login == get_field(mustjoin)) { ?>
//CODE IF OK
<?php } else { ?>
//CODE ELSE<?php } ?>
This of course is working when I have only one username in the ACF box, as soon as I put more than one username.. it doesn't work anymore because it is not an Array..
How can I create an array with ACF? what function and how can I interrogate that array?
This is the part of the code I need to show IF the current user username is in the value list ‘mustjoin’
<?php while ($day['events']->have_posts()) : $day['events']->the_post() ?>
<?php tribe_get_template_part('month/single', 'event') ?>
<?php endwhile; ?>
On another forum a guy told me to use this snippet :
function searchForName($name, $array) {
foreach ($array as $key => $val) {
if ($val['nickname'] === $name) {
return true;
}
}
return null;
}
$current_user = wp_get_current_user();
if(searchForName($current_user->user_login,get_field('mustjoin'))):
//CODE GOES HERE
endif;
But it doesn't work..
Thank you for your comment. This is the code, and it works at 99%.
<?php $current_user = wp_get_current_user(); ?>
<?php global $user_login;
get_currentuserinfo(); ?>
<?php $lookforuser = get_field('mustjoin') ?>
<?php if (strpos($lookforuser, $user_login) !== false) { ?>
<?php while ($day['events']->have_posts()) : $day['events']->the_post() ?>
<?php tribe_get_template_part('month/single', 'event') ?>
<?php endwhile; ?>
<?php } ?>
But I need to ask you if this code is "clean" or if I can make it better/faster.
Because when I open the calendar page, I can't see anything.. as soon as I refresh the page, all the events (where I am supposed to join) appear.
How can I fix it? (I tried with different computers/browsers, no changes..)
Thank you.
Thanks to another guy on another website, finally it works at 100%!
I just needed to check the user before the events loop :
<?php $current_user = wp_get_current_user(); ?>
<?php global $user_login;
get_currentuserinfo(); ?>
<?php while ($day['events']->have_posts()) : $day['events']->the_post(); ?>
<?php $lookforuser = get_field('mustjoin'); ?>
<?php if (strpos($lookforuser, $user_login) !== false)
tribe_get_template_part('month/single', 'event'); ?>
<?php endwhile; ?>
Thank you anyway for the right suggestion and help.

cakephp: how to display associated records of associated records

My objective is to display records for a related child model (once removed) in the view. I understand that setting the recursive value to '1' returns the current model, its owner(s), plus their associated models.
What I don't know how to do is display this in the view.
My approach has been to create a varialable in the controller holding the value of the associated model ID, but that hasn't work.
*I should also mention that I'm using a sluggable behavior for tournaments, so it's not $id that is used but $slug.
Here are the model view controller details:
Model
tournament - has many tournamentDetails
tournamentDetails - has many updates
updates - belongs to tournamentDetails
tournaments controller
action = view
class TournamentsController extends AppController
function view($slug = null) {
if (!$slug) {
$this->Session->setFlash(__('Invalid Tournament.', true));
$this->redirect(array('action'=>'index'));
}
$this->Tournament->recursive = 1;
$tournament = $this->Tournament->findBySlug($slug);
$updates = $this->Tournament->TournamentDetail->Update->find('all', array('conditions'=>array('update.tournament_detail_id' => $this->data['tournament_details'] ['id'] )) );
$this->set(compact('tournament','updates' ));
Tournament view. This is display the 'tournament details' and (ideally) related tournament detail 'updates'
<h2><?php echo $tournament['Tournament']['name']; ?></h2>
<?php foreach ($tournament['TournamentDetail'] as $tournamentDetail): ?>
<h1><?php echo $tournamentDetail ['title']; ?></h1>
<p><?php echo $tournamentDetail ['body']; ?></p>
<?php endforeach; ?>
<?php foreach ($updates['Update'] as $update): ?>
<h4>Update: <?php echo $update ['date']; ?></h4>
<p> <?php echo $update ['Update'] ['title']; ?></p>
<p><?php echo $update ['Update'] ['body']; ?></p>
<?php endforeach; ?>
when I debug $updates, this is all I see:
Array
(
)
I don't know what I'm doing wrong here.
Is it not correct to construct the find conditions by using:
('conditions'=>array('update.tournament_detail_id' => $this->data['tournament_details'] ['id'] )
Thanks for the help.
Paul
You can use the Containable Behavior to specify in detail what fields you want to obtain from associated models, associated models to associated models and so on. Then you will find all data inside the array $tournament, like in the examples in the link.
EDIT (in response the OP comment). I think that the problem may be using the magic findBy method. You would need to substitute that with the standard find, like in this example
$tournament = $this->Tournament->find('first', array(
'conditions' => array(
'slug' => $slug
),
'contain' => array(//Put here the settings for Containable
)
));

Resources