I want to filter posts by date where i can select multiple dates. ie 2013 & 2014.
I had been using a plugin called annual archive but this only allows you to select one, 2013 or 2014.
I've used a plugin to allow me to execute php from the text widget, so I've created a form with checkboxes and appropriate date ranges etc but wasn't sure where to send it to, so I sent a get request to a new file i made in my theme (so it wouldn't 404 and i could locate it).
I managed to process the query parameters and run a query_posts() call to get the posts. In this file, I copied a template file from my theme and now it generates with my posts.
My url is however, /wp-content/theme/.. etc which is not ideal.
The posts are all under categories, could I redirect to the /category/x page but get it to use the posts i've already found?
Is there a better way of doing things because this feels really hacked but I couldn't find a better way..
Edit: Seems like using
add_action('pre_get_posts', 'my_func');
function my_func($query){ $query->set('year', 2014); } in functions.php
is the way to go. Now I just need to figure out how to do year 2013 or 2014...
Edit 2: $query->set('date_query', [['year' => 2014], 'relation' => 'OR', ['year' => 2012]]);
Related
Apologies if this appears simple to some, but I have scaled high and low and I'm not finding a solution here to my problem, which is:
I have a website set up with Wordpress in which posts can fall under one of three categories: reviews, views, news - the slugs associated with each of these category names are the same.
Currently, calling up the web page of any individual post classified under any of these categories will see it rendered by the file single.php.
However, I want to make a slight adaption to the rendering of the post when it falls within the 'reviews' category. I have copied and renamed the original single.php file to single-post-reviews.php (no custom posts here, I will just confirm and I would like, if possible, to avoid child-theming here - not good practice, I know), but I am not seeing the new rendering from my new file.
I've also tried renaming to single-reviews.php which hasn't worked either - so could someone tell me what exactly I'm missing here?
Thanks,
WordPress Template Hierarchy for Single Posts doesn't factor in the current post category (likely due to the fact you can have multiple categories). Due to this, you have 2 viable options to your problem.
1) You can modify single.php to check for the post category, and if it's categorized under reviews, do something. This makes sense if you're just adding a small amount of markup in one or two places, or even hiding a few lines conditionally.
2) You can override the page template that's loaded based on the post's category using the single_template filter. Because I don't know exactly what you're doing, I'm going to elaborate more on this method. Take the following function:
add_filter( 'single_template', 'so51913799_review_template' );
function so51913799_review_template( $single_template ){
global $post;
if( has_category( 'reviews' ) ){
$single_template = get_stylesheet_directory() . '/single-post-reviews.php';
}
return $single_template;
}
If you put it in your functions.php file, it will make use of the has_category() function (I prefer this to in_category() since in_category just returns has_category anyways) and if it matches, it will update the $single_template variable to single-post-reviews.php. This assumes that the file is inside your /wp-content/themes/ACTIVE-THEME/ directory.
I tried the following functions in header.php, footer.php, single.php etc.
var_dump(in_the_loop());
var_dump(get_the_id());
The first function gives false (meaning we are not in the loop) and the second function gives the post id every single time.
Description of get_the_id() from wordpress :
Retrieve the numeric ID of the current post. This tag must be within The Loop.
I just want a simple explanation what the hell is going on why do i get the post id if I call the function out of the loop !?
must is a little strong for get_the_id() ...delivers evil eye to Wordpress.
It works in the header and non-loop (confirmed).
Please note that post/page are essentially interchangeable in this conversation.
Think of WP this way -> You always have a post id in some way, all the time, every page, unless you do weird stuff or talk about non-page edge cases. When you are at the install root (such as site.com/) there are posts being called, something has to be displayed. There are other settings that will impact post/page such as static front page settings. On a category listing, if there are pages, I got the first ID returned before the loop.
On post/pages the page ID is (more or less0 set before the loop. This is a result of the URL (pretty or ?p=123 format) dictating the content. Using pretty names, the page at site.com/foo-bar/ will try to look up if there is content available via the permalink rules for "foo-bar". If there is content, the post ID is obtained. (simplified)
Later in the page build you get into the loop. However, before the loop you are also offered opportunities to change, sort, or augment the loop - such as changing the page IDs to be looped or sorting.
Regarding in_the_loop(), WP says
"True if caller is within loop, false if loop hasn't started or has ended." via http://codex.wordpress.org/Function_Reference/in_the_loop
in_the_loop() evaluates if the loop is in action (loop being key to the WP world). Also important - when you are in the loop WP can iterate over multiple page/post (IDs).
I don't have a 100% bulletproof explanation as to how the ID always shows, but when you dig into the API and various methods for hooking this might be a result.
I understand your confusion and agree with you. I think WP intended get_the_id() as a loop based tool, outside the loop you will get unpredictable results.
Hope that helps, I do enjoy working in WP, and I hope you do to.
I am using WP-3.3.2 and had created a website eyepractice my script gets month and year from url and then shows calendar for that month and year but when I integrate with Wp it only work for year 2012 and if I use http://www.eyepractice.ca/optometris/guelph/?month=1&year=2013 it shows Page Not Found however I have already created a Page named guelph from Wp admin. I searched whole project for 2012 but it is not hard coded.
This is a REALLY old question, but I came across it while looking for solution to the exact same problem, so I will leave an answer here.
I used a calendar creation script based on this link:
http://davidwalsh.name/php-event-calendar
In Wordpress apparently using 'year' and 'month' as $_GET variables conflicts with Wordpress internal query variable handling, so the calendar only worked for current year for me and threw a 'page not found' error on the next year.
The solution was simple. As #janw suggested, change the parameter names. In the script wherever $_GET variable is called 'month' or 'year', change it to something else like 'cal_month' or 'cal_year'. Works like a charm.
I'm developing a Wordpress plugin that requires updates, although the plugin version is being checked from my server. There are several plugins that I have developed which use the exact same server to check for new versions. The problem I'm experiencing is that when all the plugins require an update and I click View Details, one of the plugins will show details of the update (version, description, and etc), but the other plugins won't show any information. After some debugging I can see that the server is returning data for sure.
My question is, how can I apply the plugins_api filter multiple times without it conflicting with the other plugins?
Your observation is right. It is not obvious. Even the book of Brad and Ozh (Plugin development ed. Wrox) includes an error in the example on page 267 in the chapter "make your own API repository".
Like you, I spent (lost) time to find the issue with a two plugins in alternate API...
The solution:
Remember that that first parameter in the WP filter is the original value passed to the filter.
So to concatenate the filters (listed by plugins using alternate api)... the first line must be:
function xiliw_altapi_information( $false, $action, $args ) {
$plugin_slug = plugin_basename( __FILE__ );
// Check if this plugins API is about this plugin
if( $args->slug != $plugin_slug ) {
return $false; // var to conserve the value of previous filter of plugins list in alternate api. fixes book error not val false
}
// POST data to send to your API
$args = array(
'action' => 'plugin_information',
'plugin_name' => $plugin_slug,
'version' => $transient->checked[$plugin_slug],
'registration' => $this->registration
);//../..
By doing this test, each time the list of hooks is called, only one - the concerned plugin - gives the right answer to display the information for the splash window.
If I have time, I probably will publish soon a more complete article about a class to manage this alternate powerful API and how to add it to a -private- plugin.
I have 2 websites with similar content types.
Let's say an event-content type with some cck fields in it.
site1: events ( title, body, image )
site2: events ( title, body, image, onsite1)
by the extra field at site2-events i want to give the possibility to the user to post his event on the ( main ) site1.
Site1 and site2 are both on 1 database, although tables from site2 are prefixed.
How can i add content made from site2 to site1?
( Is there an easy way to do this without resorting to sql? I am using the nodeapi at this time to do some extra when an event is submitted. )
Since you say you're already using hook_nodeapi it seems like you could just do:
if ($op == 'insert' && (see if checkbox is checked here))
...then switch to the other site's database, do a node_save, and switch back to the current site's database to let Drupal finish its business.
You might be able to use db_set_active() as Mike-Crittenden describes even if it is within same database, as both $db_url and $db_prefix can be arrays, instead of single strings.
This way you can have the same db_url for both 'default' and 'alternative' db, but use different prefixes to switch between databasees using db_set_active('alternative') and db_set_active() to return to default.
Lots of people use Feed API / Feeds module for this. You can filter by taxonomy terms, content type, whatever you need so that you don't have to import everything from the primary site. Great tool.