WP_Query with WPML returns different results on custom post types pages - wordpress

I am using Wordpress with WPML plugin (2 languages). I execute following WP_Query in header of all pages to get the results of my custom post type (slideshow):
$slideshow = new WP_Query(array( 'post_type' => 'slideshow', 'showposts' => 20 ));
Query works fine on homepage, posts, pages, archives and returns last 20 items from my custom post type "slideshow" in CURRENT language. But when I visit my custom post type page (e.g. /custom-post-type/post-name) the same query returns last 20 items from ALL languages which is wrong!
What can I do? I also tried experimenting with supress_filters true/false as suggested on various forums around the web but with no luck - it returns the same posts in any case.

The problem was in custom post slugs translation...
To fix this navigate to WPML > Translation management > Multilingual content setup and UNCHECK "Translate custom posts slugs (via WPML String Translation)"

Related

Singular/Plural URL Structure in WordPress for Post Types

I am working on a client's website, and he has asked me a very genuine question, and I am unable to figure it out.
He is Creating a Website for Piano Chords, where he will write tutorials about chords in general and each individual chords too. And he wants them to be of a similar post type.
So I registered a CPT Chords with the slug chords using the documentation on Codex.
Now the problem is the site has multiple posts about chords. And they will be of 2 kinds.
One will be generic to all chords, hence the word needed in the slug is chords and not the singular form.
e.g. -> https://mypianonotes.com/chords/overview/
This is fine. Now problem is when we need content like "C Major Chord". This is supposed to be singular. So the slug/url should be chord/c-major/ and not chords/c-major/.
Is there any solution to this problem? Or will I have to create 2 separate post types?
Register the post type as 'my_chords' then add a rewrite rule parameter in the arguments like this
<?php
$args = array(
'rewrite' => array('with_front'=>false, 'slug'=>'chord'),
);
register_post_type( 'my_chords', $args );
?>
Then create a page for 'chords' with a child page of 'overview'.
Hope this helps :)

Wordpress thinks custom taxonomy page is search page

I have set up a custom taxonomy for regular posts called guide.
I have a template file called taxonomy-guide.php which lists out posts with a guide taxonomy.
So my URL structure appears like this: http://www.example.com/guide/new-york-city.
This all works fine and dandy, the pages are listing out the posts categorized with the correct guide.
However, this is the issue, the page title on those URLs is You searched for | My Site Name, which is what it should be only on search pages.
Relevant, I'm using the Yoast SEO Plugin, and it even has an area to edit the Titles and Meta for your taxonomies, which I have, and the changes there are not taking effect, no matter what I put there it's still showing You searched for | My Site Name.
Do you think there is some sort of URL structure conflict, or what could be causing this issue?
Update
I have disabled the Yoast SEO Plugin and now the titles on those pages are | Search Results | My Site Name, so it still thinks it's a search results page.
2nd Update
I have this at the top of my taxonomy-guide.php file in order to update the sort order:
$args = array_merge( $wp_query->query_vars, array( 'orderby' => 'date', 'order' => 'ASC', 'parent' => 0 ) );
query_posts( $args );
If I remove this, the page title is fixed, although now the posts aren't in the order I want. Any idea why this would be causing the page title to change? Or how I can change the order a different way to see if that fixes it?
Solution
If I change the above code in the 2nd Update to
query_posts($query_string . '&orderby=date&order=ASC&parent=0');
It works as expected. Not sure what was causing the page title to change in the code in my 2nd Update, but that's an odd one.

Custom WP_Query not working (listing featured articles + popular articles)

I am Wordpress beginer and I want to set multiple queries on a page that lists my posts.
Featured posts
I have front page that lists featured posts from all categories,
Categories pages that lists featured posts from current category
Q: Is it better to use My featured posts new category name or to set sticky post (under public - publish options) for posts I want to feature on front page and on categories pages? Every post already have his own category like News so the My featured posts will be the second category.
Query
Let's suppose I'm using category name for featured posts (but then i got permalink to My featured posts category (site/my-featured-posts/2013/07.....) which I don't want (so maybe sticky posts are better solution)
I'm trying to setup query to list featured posts but only from standard post type, not gallery or video
<?php
$arg = array(
'cattegory_name' => 'my-featured-category',
'posts_per_page' => 5,
'nopaging' => true,
'post_status' => 'publish',
'post_type' => 'post'
);
$featured= new WP_Query($arg);
if ($featured->have_posts()):
while ($featured->have_posts()) :
$featured->the_post();
?>
and then below the_title(); ..... and so on
What I get is all articles from all categories.
Q: Also, how to I get popular articles based on views and number of comments for the last day?
Q: How to list posts which have post format video?
Q: Are there any online tools that build wp_query based on criteria?
Thank you.
You can also set posts as "featured" using a tag. You can then use tag=featured with WP_Query to get posts tagged with a certain tag ("featured" in this example).
As for your additional questions...
Q1: You can save page views to a flat file or to the WordPress database. Once I used the database to store a UNIX-timestamp as traditional WP post meta for each post page view. As it was a timestamp, I could calculate the age of the page view easily (and I could also run SQL queries to remove post meta that was older than a certain timespan).
Comments can be queried by time ordering too. See WP_Comment_Query.
Of course there are plugins that can do this for you, but for me they included useless bloat I did not need.
Q2: For querying post formats, use the query variable called tax_query as GATEKeeper did in a WP support question: http://wordpress.org/support/topic/post-formats#post-2034414.
Q3: A google query returned a thing called The WordPress Query Generator.

How to make your own post format in Wordpress?

How can I create my own custom post formats?
Or how can make my custom post type make work with a function like
get_post_format();
For example i have a custom-post type with the type of "accordion" and i like to be able to use it with as content element in the loop, but only if it exists...
get_template_part( 'content', get_post_format() );
So i am looking for a function like
get_custom_post_format();
which does not exists in Wordpress.
Anybody tried something similar?
I'm not sure if you're asking how to create custom post formats or custom post types so I've provided the answer to both.
If you're asking whether you can create custom post formats...
...then the answer is no. See the quote below from Post Formats on the WordPress codex:
The Post Formats feature provides a standardized list of formats that are available to all themes that support the feature. Themes are not required to support every format on the list. New formats cannot be introduced by themes or even plugins. The standardization of this list provides both compatibility between numerous themes and an avenue for external blogging tools to access this feature in a consistent fashion.
If you're asking how to create a custom post type:
The most basic example of creating (registering) your own custom post type is to add the following code to your functions.php file inside your theme.
function register_recipes_post_type() {
$args = array( 'public' => true, 'label' => 'Recipes' );
register_post_type( 'recipe', $args );
}
add_action( 'init', 'register_recipes_post_type' );
The above code hooks our register_recipes_post_type function to be executed when the init action is triggered by WordPress core.
Once you've added this code, if you go to your wp-admin you'll see a new menu on the left called 'Recipes', that's your new custom post type. If you add a new recipe, give it a title and some content, publish it and then try to preview it, you'll notice that you get a 404 error. After creating a new custom post type you need to go to your Settings > Permalinks in your wp-admin, just visiting that page will fix your permalinks to include the new custom post type so if you now go back and refresh the preview of the recipe you just created you'll see that it now works rather than 404s.
Now if you create a new file called single-recipe.php and put some code inside it, just put 'test' now for the purpose of seeing that it works and once you have, refresh the preview of the recipe you just created once again and you should see that it just displays the word 'test'. Using that file you can create a completely custom template to be displayed for showing single entries (posts) of that custom post type, or you could use content-recipe.php if your single.php includes a get_template_part( 'content', get_post_format() ); as you said in your original post.
Obviously your custom post type probably won't be for recipes but just update instances of recipe and recipes to whatever you want it to be.
There are also other post type specific templates you can create too for your archive of the post type etc. The above should be enough to get you started though.
There are also other arguments you can pass when registering your post type, you can see the full list here: http://codex.wordpress.org/Function_Reference/register_post_type
I hope this helps. Good luck! =)
Creating New post format is not allowed currently by WordPress. you can’t define any post format apart from what WordPress allows.
Reference:
1. http://wp.tutsplus.com/tutorials/proof-using-post-formats/

In WordPress how do you register built-in taxonomies with custom post types in code?

The WordPress codex has a lot of examples of how to register custom taxonomies with custom post types, but I couldn't find much about using built-in taxonomies (tags & categories) with cpts.
I have a cpt called listings, and I need to add the standard category and tag UI elements to the listing cpt page. I also need to do this with code in my functions.php, rather than using a plugin.
Not a problem at all. When you register the post type, just add this argument to the array:
'taxonomies' => array( 'category', 'post_tag' )
Suppose you defined your cpt (custom post type) by the following:
register_post_type('listings', $args); // where $args is an array of your cpt settings
Then you could use the following to add taxonomy:
// category-like:
register_taxonomy('listing_category', array('listings'), array('hierarchical' => true, ...));
// tag-like:
register_taxonomy('listing_tag', array('listings'), array('hierarchical' => false, ...);
In fact, I personally put those custom type definitions in my own plugin (not open for public as it provide my own site functionalities, which obviously not suit the others at all).
The problem of putting in functions.php increases the difficulty to change to a new theme (although changing theme is not so often, but for self-owned blog, it do happen in some day).
Moreover, the custom post types should be site-wide, not depending on the current theme. So semantically it should not be in the theme's directory.

Resources